0

I'm working on pset2 from cs50 and am down to the last bug which I can't seem to wrap my head around. When I read my code, I AM using the parameter argv in line 3. Why the error message?

int main(int argc, string argv[]) // Will store argv[0] = ./caesar and argv[1] = key. Improve, check that argv[1] is only an int
{
    int key = atoi("argv[1]");

if (argc != 2 || key <= 0)// Check that argv[0] is the file name as well? converts "key" (string) into int value
{
    printf("Try again.");
}

string text = get_string("Plaintext: ");// Prompts user for plaintext

printf("Ciphertext: ");// Prints ciphertext

for (int i = 0, n = strlen(text); i < n; i++)// Example of good design bc doesn't have to check the length of the string on every iteration
    {
        if (isalpha(text[i]) == true && isupper(text[i]) == true)
            {
                int j = (int) text[i] % 65;
                int k = (j + key) % 26;
                char l = (char) k + 65;
                printf("%c\n", l);
            }
        else if (isalpha(text[i]) == true && islower(text[i]) == true)
            {
                int j = (int) text[i] % 97;
                int k = (j + key) % 26;
                char l = (char) k + 97;
                printf("%c\n", l);
            }
        else
            printf("text[i]");
    }
}
1

Actually, no, you're not using argv[1].

    int key = atoi("argv[1]");

By putting it in double quotes like that, you're telling the code to use the literal string "argv[1]" instead of the var. It amounts to doing this:

    int key = atoi("fuzzy kitten");

By surrounding anything in double quotes, it means to use exactly what is in quotes as a literal character string.

Just drop the quotes.

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

1

int key = atoi("argv[1]"); this is using the literal string "argv[1]", not the value stored in argv[1].

Also, you should be checking that argv[1] exists before you use it as an argument to atoi (ie, check that argc == 2 first).

You must log in to answer this question.

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