1

Currently I am working on Caesar and validating the user inputs. The argument count section works fine. However, I can't find a way to compare the value in the vector argument string (argv[1]) to see if it is an integer.

What I have written in line 3 is incorrect, but is what I want to do. Can someone please give some advice on what syntax would work here?

 int main(int argc, string argv[])
{
     for (argc == 2 || argv[1] == int)

    {
        puts("Success");
        printf("%s\n", argv[1]);
    }
    else
    {
        printf("Usage: ./caesar key\n");
        return 1;
    } 

** Updated ** Thanks for the help. I have used the isdigit function as suggested and converted it's input from the argv string to an integer. This program now has some unexpected behaviour, even tho it passes all the checks as follows:

$ ./caesar
Usage: ./caesar key // correct
$ ./caesar -9
Usage: ./caesar key // correct
$ ./caesar a
Usage: ./caesar key
$ ./caesar 50
Success // correct
50
$ ./caesar 40
Usage: ./caesar key // incorrectly rejects?

** If this is an issue with isdigit not iterating over the array, then my apologies, I will work on that next.

int main(int argc, string argv[])

  // If the command line arguments are not equal to 2, then print Usage: ./caesar key and return 1
  if (argc != 2)

{
  printf("Usage: ./caesar key\n");
  return 1;
}

  // Int k converts to an integer 
  int k = atoi(argv[1]);

  // check if the integer is non-negative
   if (k < 0)
{
   printf("Usage: ./caesar key\n");
   return 1; 
}
   // Check if argv[1] is an integer, if not, print Usage: ./caesar key and return 1
   int j = atoi(argv[1]);

   if (isdigit(j) == 0)
{
   printf("Usage: ./caesar key\n");
   return 1; 
}

   else
{
    puts("Success");
    printf("%s\n", argv[1]);

} 
2

I do not remember now if such a check is necessary, anyway if you want to check if a character is integer we have the function isdigit (), you can search in google about it. I would separate the checking tasks in two: if the arguments are two, and if the second argument is an integer, for that we just have to do something like:

for(int i = 0, n = strlen(argv[1]); i < n; i++)
{
    if(!isdigit(argv[1][i]))
    { 
        return 2;
        ......

Remember that arv [1] is a string, the parameter that we will pass to isdigit is a character (thanks @Blauelf for the clarification, it is incredible how the most elementary things are forgotten)

EDIT: Given the new question, I edit my answer. We should not use isdigit with an integer, since it will give us an incorrect answer or a segfault. isdigit checks the ascii value associated to the character, therefore your program is not correct, first we iterate over argv [1], as indicated above, and only later we convert to an integer with atoi, in this order. The peculiarities of a poorly implemented function (really isdigit is a macro) can be a headache

5
  • The caesar check was added last fall/this year. Note that isdigit and its siblings are infamous for not generating compile errors if you pass a non-char, due to how they are implemented, so double check you are not accidentally passing a char*, or string. One digit at a time. – Blauelf Apr 8 '19 at 7:11
  • Ok great, thanks for that. I implemented the function and yet it only works with certain values of integer inputs, which I can't explain. Any hints on troubleshooting this? – Andrew Alton Apr 8 '19 at 22:01
  • You can read my edit – MARS Apr 8 '19 at 22:19
  • Cheers for that - it's working now. – Andrew Alton Apr 9 '19 at 19:01
  • Glad to hear it – MARS Apr 9 '19 at 19:30

You must log in to answer this question.

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