I'm working on Caesar from Problem Set 2. I have done the first step to check for a single command line argument, but I'm stuck on how to check the argument to see if any characters are non - decimal digits. My thought was to iterate through the array and use isdigit() to check, but it doesn't seem to work and I'm not sure why. This is the current code I have:

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

int main(int argc, string argv[])
    //Check for single command line argument
    if (argc > 2)
        printf ("Usage: ./caesar key\n");
    for (int i = 0; i < argv[]; i ++)
        if (isdigit(argv[i]) != 0)
            printf("Usage: ./caesar key\n");


Two problems. First is the for loop setup. "argv[ ]" cannot be used like this. It requires an index inside the square brackets to select one of the array elements (one of the parameters given when the program is called.)

Since you're trying to check each char in the first parameter given when the program is executed, it should be "argv[1]".

Second problem: The code is trying to check that each char in the string is a digit. isdigit() can only check single chars. If given an entire string, it will seg fault. "argv[1]" is an entire string. The code needs to select each char from the string, i.e., "argv[1][i]".

On another note, you believe you have the check for the correct number of parameters done? Two questions:

  1. What happens if the program is called without any parameters?
  2. What terminates the program if the number is incorrect?

Happy programming. ;-)

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

You must log in to answer this question.

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