I'm trying to check whether argv[ 1 ] is an integer by looping through every character in argv[ 1 ] and putting them in isdigit(). Technically it works but not exactly the way I want it to.
Here's my code.
int main(int argc, string argv[])
{
if (argc != 2)
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
for (int i = 0; i < strlen(argv[1]); i++)
{
if( !isdigit(argv[1][i]))
{
printf("Usage: ./caesar key\n");
return 1;
}
else
{
printf("Sucess\n");
printf("%s\n", argv[1]);
}
}
}
}
As you can see by looping through the string the program prints out a new line of response for each character. I'm wondering whether there's another way to validate the input and only print the response once?
$ ./caesar 20
Sucess
20
Sucess
20
$
$ ./caser xyz
bash: ./caser: No such file or directory
$ ./caesar 20
Sucess
20
Sucess
20
$ ./caesar xyz
Usage: ./caesar key
$ ./caesar 20x
Sucess
20x
Sucess
20x
Usage: ./caesar key
$