0

Everything else in my program seems to run smoothly but I always get the problem of ":( Handles non-numeric key" but when I switch the atoi function with an isdigit function, it passes the handles non numeric key but everything else fails. please help. thank you

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

int main(int argc, string argv[])
{
        if (argc == 2 && atoi(argv[1]) > 0)
    {
        string s = get_string("Plain text: ");
        printf("ciphertext: ");
        int k = atoi(argv[1]);
        int l = strlen(s);

        for (int i = 0; i < l; i++)
    {
        if (isalpha(s[i]))
        {
            if (isupper(s[i]))
            {
                printf("%c",(((s[i] - 65) + k) % 26) + 65);
            }
            else if (islower(s[i]))
            {
                printf("%c",(((s[i] - 97) + k) % 26) + 97);
            }
        }
        else
        {
            printf("%c", s[i]);
        }
    }
    printf("\n");
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    }
}
1

Didn't the spec say that the code should test for non-numeric keys and should print an error message when a non-numeric is found?

Perhaps you need to validate the key as all numeric, and then use atoi()

2
  • sorry but I still can't understand how to solve this. How excatly would I validate they key before using atoi. I tried setting up another if statement before the first one and it still did not work. When I put in a non numeric it prints an error but when i do check50 it doesnt work. – E V May 14 '20 at 5:14
  • Didn't you just say that you used isdigit to make the one test pass? And atoi to make all the others pass? How would you use BOTH of those sets of code, in the same program, to make all the tests pass? I'm trying to get you to work it out without me telling you! "Teach a man to fish and you've fed him forever." – Cliff B May 14 '20 at 5:19

You must log in to answer this question.

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