I am trying to get the code to check that: 1) Only a numerical argument is passed through 2) If a non-numerical, number is pass through, an error message is revealed to the user.
Here are the wording of the problem statement:
Modify caesar.c at right such that instead of printing out the command-line argument provided, your program instead checks to make sure that each character of that command line argument is a decimal digit (i.e., 0, 1, 2, etc.) and, if any of them are not, terminates (with a return code of 1) after printing the message Usage: ./caesar key
Once saved, print out the integer, as via %i with printf. So, for example, the behavior might look like this:
Option 1
$ ./caesar 20
Success
20
or
**
Option 2
$ ./caesar 20x
Usage: ./caesar key
Here is the issue. Regardless of whether i input a number(20), or a number-digit ( 20x), I get a successful response, which shouldn't be the case. In fact, I should be reprompted for Usage:./caesar key when I input "20x"
Here is my line of code, and I really appreciate any and all help that I can get :
# include <cs50.h>
# include <stdio.h>
# include <ctype.h>
# include <string.h>
int main(int argc, string argv[])
{
if (argc == 2)
{
printf("Success\n%s\n", argv[1]);
return 0;
}
else
{
printf("Usage: ./caesar key\n");
return 1;
}
string s = argv[1];
for (int i=0, n = strlen(s); i < n ; i++)
{
if(isdigit(s[i]))
{
continue;
printf("test");
}
else
{
printf("Usage: ./caesar key \n");
return 1;
}
}
}