0

I am currently at CS50x2020 PSET 2 Caesar "validating the key" and having issue with using the isdigit function. From what I understand from the programmer's manual, isdigit will return 0 if the input is not a digit. Yet in my case, my code only returns the correct output if I code the following: (!=) instead of (==). Am I missing anything? Thanks in advance!

 int k = atoi(argv[1]);
    for (int i = 0, n = strlen(argv[1]); i < n; i++)
    {
        if (isdigit(k) != 0)
        {
            printf("Usage: ./caesar keys\n");
            return 1;
        }
...

1

isdigit(k) is testing whether the ascii value of k is in the range of 48 - 57. Those are the ascii representations of '0' through '9'; here is an ascii table for your review. But k is an integer from this declaration int k = atoi(argv[1]);, therefore isdigit(k) always returns 0.

Program should test that each character of argv[1] is a "digit", ie argv[1][i]. Once that is verified, it would be ok to convert it to an integer.

1
  • Got it! thank you so much!!
    – iluvexcel
    Mar 14 '20 at 13:56

You must log in to answer this question.

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