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]);
}