slrpeee03
slrpeee03

Reputation: 25

cs50 pset2 caesar---validating the key

I'm going through cs50 and I'm stuck on pset2 caesar. I'm taking it step-by-step as instructed and I got past the part about validating the key--making sure the command-line argument is only a number (./caesar 20) and if not (./caesar 20x or ./caesar xyz) to print out Usage: ./caesar key\n. But once I add the next lines to prompt the user for plaintext and then print back the ciphertext it doesnt seem to register the former lines that validate the key. I am aware of Harvard's academic integrity policy so I'm more asking for hints/help/explaination with this. I've posted my code below and any help is greatly appreciated.

int main(int argc, string argv[])       
{

        int i = 0;
        if(isalpha(argv[1][i]))
        {
            printf("Usage: ./caesar key\n");
            return 1;
        }
        else
        {
            int key = atoi(argv[1]);
            string text = get_string("plaintext: ");
            printf("ciphertext: ");
            int n = strlen(text);
            for(i = 0; i < n; i++)
            {
                if(isupper(text[i]))
                {
                    printf("%c", (((text[i] - 65) + key) % 26) + 65);
                }
                else if(islower(text[i]))
                {
                    printf("%c", (((text[i] - 97) + key) % 26) + 97);
                }
                else
                {
                    printf("%c", text[i]);
                }
            }
    }
    printf("\n");
    return 0;
} 

Upvotes: 1

Views: 1241

Answers (2)

Dion
Dion

Reputation: 5

You have to check 2 things in the command-line argument. 1. Check if you have 2 arguments. (This is not checked in your program) 2. Check if it is not an alphabet... Since the second argument is automatically a string (due to string argv[]), you must convert it first to integers. ---- this breeds sub-problems that you must take care of by: 2.A. Check the string one-by-one (per CHARACTER) and all must be an integer. 2.B. If at least one is a character then return false...

2.A. and 2.B. should be a nested in a separate function that would return a boolean value(T/F), then from that boolean value, enclose those two boolean expressions in the while loop.

2 months late on my answer but since I just finished this problem, I thought maybe someone might be experiencing the same difficulty I had with this so hope someone can read this and help them out. :)

Upvotes: 0

DinoCoderSaurus
DinoCoderSaurus

Reputation: 6520

You need to rework the input validation.

  1. It does not ensure that one argument is supplied at the command line.
  2. It is only validating the first character of argv[1]. Program should test that every character of the argument is a digit (see man isdigit). isalpha() is not a good test. What if user enters $?

It is not clear when program doesn't work, but this will not pass check50 based on these defects.

Upvotes: 1

Related Questions