So I´ve been stumped on this for hours. I´ve used a loop and "isdigit" to check each character of argv[1] to make sure its a numerical digit, yet it is not checking properly.

As my code is now if the loop reaches a number it prints "success" regardless of the other alphabetical characters in the string. For example, both ./caesar 20 & ./caesar 20x return a "success"\n when the later should fail the check.

I had suspected the problem is with the loop and that the answer might, somewhere, involve the command atoi considering the hints call for it and I have yet to use it in my code, although I'm new at this, obviously, and could be wrong.

Here is my code. Please help as best you can as I´ve spent too much time working on this and would like to move on. Also I apologize for any grammatical errors. I made this post while on break at work.

Thanks :)

#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
    int main(int argc, string argv[0])
    // First, Make sure we have a key, if not, try again.
    if (argc != 2)
        printf("Use ./caesar key\n");    
        return 1;
    //Second, check to if it is a digit.
    for (int i = 1; i < argc; i++)
        for (int key = 0, n = strlen(argv[i]); key < n; key++)
            // the loop interates over the whole array. If one nuber is detected 
            // at all it returns a "success"
              if (isdigit(argv[1][key]))
                  printf("Success\n %s\n", argv[1]);
                  return 1;
                printf("Please only use numbers\n");
                return 0;


1 Answer 1


Your return 1; will immediately end the program (ie, if the first character is a digit, your program will print success) so no further characters are checked.

You should reverse how you are checking the characters ... if it's not a digit, then print the error and return; otherwise, keep checking and if you reach the very end of the loop without it stopping, then you know the key is valid.

  • This was helpful. I've gotten around this by using isalpha funciton but it feels like cheating in a way. For example if I send a command ./caesar 20! if fails to recognize "!" as not a digit. I cannot seem to figure out how to write "x is not digit" in c using ctype. Would it be better if I set it to a boolean expression or scrapped ctype for an ascii formula? Mar 19, 2019 at 15:55
  • if (isdigit('x')) is false. if (!isdigit('x')) is true.
    – curiouskiwi
    Mar 19, 2019 at 19:06
  • What is the difference between if (isdigit('x')) and if (!isdigit('x'))? What does the exclamation point do?
    – abhi810
    May 15, 2020 at 4:11
  • @abhi810 the ! is "not". so (!isdigit('x') so isdigit('x') will be false, since 'x' isn't a digit, therefore !(false) is true.
    – curiouskiwi
    May 17, 2020 at 4:06
  • @curiouskiwi when an alpha character is encountered, the previous numeric character has been already passed. you either get a pass plus error or only a pass. in my case neither !isdigit() nor isalpha() works, I just get success and no errors for the input like 4k - 'k' is not caught but '4' is passed :(
    – stash4uk
    Nov 5, 2020 at 8:04

You must log in to answer this question.

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