So I´ve been stumped on this for hours. I´ve used a loop and "isdigit" to check each character of argv[1] to make sure its a numerical digit, yet it is not checking properly.
As my code is now if the loop reaches a number it prints "success" regardless of the other alphabetical characters in the string. For example, both ./caesar 20 & ./caesar 20x return a "success"\n when the later should fail the check.
I had suspected the problem is with the loop and that the answer might, somewhere, involve the command atoi considering the hints call for it and I have yet to use it in my code, although I'm new at this, obviously, and could be wrong.
Here is my code. Please help as best you can as I´ve spent too much time working on this and would like to move on. Also I apologize for any grammatical errors. I made this post while on break at work.
Thanks :)
#include <cs50.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int main(int argc, string argv[0])
{
// First, Make sure we have a key, if not, try again.
if (argc != 2)
{
printf("Use ./caesar key\n");
return 1;
}
//Second, check to if it is a digit.
for (int i = 1; i < argc; i++)
{
for (int key = 0, n = strlen(argv[i]); key < n; key++)
{
// the loop interates over the whole array. If one nuber is detected
// at all it returns a "success"
if (isdigit(argv[1][key]))
{
printf("Success\n %s\n", argv[1]);
return 1;
}
{
printf("Please only use numbers\n");
return 0;
}
}
}
}