0

I'm having some issues with Caesar. I can't seem to find anything wrong with my code, however, when I run the program, it prints things that seem to make no sense. When I run check50 on it, it's detecting null characters.

Here's my block of code.

    int main(int argc, char *argv[])
    {
        string alpha = "abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz";
        string alphaup = "ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUVWXYZ";
        int key = atoi(argv[0]);
        int c = 0;

        string input = GetString();
        for(int i = 0; i < strlen(input); i++)
        {
            if(isalpha(input[i]))
            {
                if(isupper(input[i]))
                {
                    c = input[i] + key;
                    printf("%c", alphaup[c]);
                }
                else
                {
                    c = input[i] + key;
                    printf("%c", alpha[c]); 
                }

            }
            else
            {
                printf("%c", input[i]);
            }
        }
        printf("\n");
    }

When I run the code, it will print something like this

~/workspace/pset2/ $ ./caesar 13
asdf
S;VX

Here are my error messages

:) caesar.c exists
:) caesar.c compiles
:( encrypts "a" as "b" using 1 as key
   \ expected output, but not "S\n"
:( encrypts "barfoo" as "yxocll" using 23 as key
   \ expected output, but not "TS\u0003X\u0000\u0000\n"
:( encrypts "BARFOO" as "EDUIRR" using 3 as key
   \ expected output, but not "\u0000\u0000ÿ\u0000pp\n"
:( encrypts "BaRFoo" as "FeVJss" using 4 as key
   \ expected output, but not "\u0000Sÿ\u0000\u0000\u0000\n"
:( encrypts "barfoo" as "onesbb" using 65 as key
   \ expected output, but not "TS\u0003X\u0000\u0000\n"
:( encrypts "world, say hello!" as "iadxp, emk tqxxa!" using 12 as key
   \ expected output, but not "\u0000\u0000\u0003\u0000V, ;S\u0000 ZW\..."
:( handles lack of argv[1]
   \ expected output, not a prompt for input
https://sandbox.cs50.net/checks/9c1acc5a6fce4787bd067703a37bce2d

Any tips on how to fix this?

1 Answer 1

1

This may not be the only problem, but I stopped reading here: int key = atoi(argv[0]);. From the transcript of the Command-Line Arguments Short in Week 2:

This means that each element in argv, starting from zero, contains the command and arguments. For example, argv[0], which I'll refer to as argv zero, will always contain the command that is being run-- .......

You must log in to answer this question.

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