0

(Updated on 20170607. Please scroll down for the update)

I wrote the code about a year ago and it passed check50 before but I changed job so stopped the course for a while.

Now I resubmitted the program and it wouldn't pass check50 anymore.

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    // check arg number
    if (argc == 2 || (int) argv[1] > 0) 
    {
        string msg = GetString(); // message
        int i; // loop for all characters
        int n; // number of charaters
        int key = atoi(argv[1]); // key to encrypt

        // "rotate" all characters by "key"
        for (i = 0, n = strlen(msg); i < n; i++)
        {
            int p = (msg[i] + key % 26); // character to print

            // if input is symbols, print changed
            if (msg[i] < 'A') 
            {
                printf ("%c", msg[i]);
            }

            // if output outside of a-z & A-Z, rotate it back around and print
            else if (p > 'z' || (p > 'Z' && p < 'a')) 
            {
                printf ("%c", (char) (p - 26));
            }

            // if output within a-z or A-Z, print "rotated" output
            else
            {
                printf ("%c", (char) p);
            }

        }
        printf ("\n");
        return 0;
    }

    // if too many arguments or input isn't a +int, yell and return 1
    else
    {
        printf ("Usage: /home/cs50/pset2/caesar <number>\n");
        return 1;
    }
}

Even the "world, say hello!" got encrypted correctly:

~/workspace/pset2/ $ ./caesar 12
world, say hello!
iadxp, emk tqxxa!

However check50 comes back with this:

:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
   \ expected output, not a prompt for input
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, not a prompt for input
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, not a prompt for input
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
   \ expected output, not a prompt for input
:( encrypts "barfoo" as "onesbb" using 65 as key
   \ expected output, not a prompt for input
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
   \ expected output, not a prompt for input
:) handles lack of argv[1]

Could anyone please tell me what went wrong?

P.S. I am a REAL BEGINNER and English is my third language. Please try to explain in very simple language. Thank you!

===

20170607 update:

Added a line to comply with the plaintext: and ciphertext:. Here is the new code:

#include <stdio.h>
#include <cs50.h>
#include <string.h>
#include <stdlib.h>

int main(int argc, string argv[])
{
    // check arg number
    if (argc == 2 && (int) argv[1] > 0) 
    {
        string msg = GetString(); // message
        int i; // loop for all characters
        int n; // number of charaters
        int key = atoi(argv[1]); // key to encrypt

        printf("plaintext:  %s\nciphertext: ", msg);

        // "rotate" all characters by "key"
        for (i = 0, n = strlen(msg); i < n; i++)
        {
            int p = (msg[i] + key % 26); // character to print

            // if input is symbols, print changed
            if (msg[i] < 'A') 
            {
                printf ("%c", msg[i]);
            }

            // if output outside of a-z & A-Z, rotate it back around and print
            else if (p > 'z' || (p > 'Z' && p < 'a')) 
            {
                printf ("%c", (char) (p - 26));
            }

            // if output within a-z or A-Z, print "rotated" output
            else
            {
                printf ("%c", (char) p);
            }
        }
        printf ("\n");
        return 0;
    }

    // if too many arguments or input isn't a +int, yell and return 1
    else
    {
        printf ("Usage: ./caesar k\n");
        return 1;
    }
}

Now my code ONLY WORKS SOMETIMES:

~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k
~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k
~/workspace/pset2/ $ ./caesar 13
world, say hello!
plaintext:  world, say hello!
ciphertext: jbeyq, fnl uryyb!
~/workspace/pset2/ $ ./caesar 13
Usage: ./caesar k

Could anyone please help take a look at the program and tell me what went wrong? Thank you!

1 Answer 1

2

Every year, CS50 changes what check50 is looking for. You are using the 2016 version - you need to change to what the 2017 check50 is looking for, which is this:

~/workspace/pset2/ $ ./caesar 12
plaintext: world, say hello!
ciphertext: iadxp, emk tqxxa!

Can you see the added plaintext: and ciphertext:? To see the 2017 instructions, go here. Basically, you need to make sure that you are following the new CS50 instructions.

5
  • Thank you for the answer. Now my problem is that when I run the program (e.g. ./caesar 13), the program sometimes allows me to type in the plaintext and sometimes prints "Usage: ./caesar k", while everything is exactly the same. Why does that happen? Thanks again for helping!
    – Jackie SOO
    Commented Jun 6, 2017 at 11:33
  • No problem! Could you provide your new code above? I have an idea what the problem might be, but I can't say for certain without seeing the code. Thank you! :-) Commented Jun 6, 2017 at 11:43
  • The code wouldn't fit in the comment. Please see the "20170607 update" in the original question. Thanks!
    – Jackie SOO
    Commented Jun 7, 2017 at 6:58
  • The problem is if (argc == 2 && (int) argv[1] > 0). For some reason, (int) actually isn't making argv[1] an integer 100% of the time. I've tried to look this up, but nothing says anything about (int) not working. I even used (int) in a test program and it worked, so I'm not sure why it wasn't working with you. What I did find, however, was that using atoi() worked all the time. I also found that deleting (int) argv[1] > 0 and adding an if ( key > 0 ) below key also worked 100% of the time. I'm really sorry that I don't have a certain answer for you! (continued) Commented Jun 7, 2017 at 14:26
  • A million thanks @SuperNovaCoder! After splitting up some parts and a little rearrangement now the program works EVERY TIME. (And thank you for the tip on plaintext: . I totally misunderstood what they were asking for.)
    – Jackie SOO
    Commented Jun 7, 2017 at 15:43

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .