How do I fix this. My program must not accept any alphabet.
Int must accept only valid numbers and reject the string if it contains a alphabet
int main(int argc, char *argv[])
{
char str[1000];
int i, ch, key;
{
if (argc != 2)
{
printf("key\n");
return 1; // exit prog
}
// read argv value into key after argc valid
//to prevent sementation error
key = atoi(argv[1]);
// do multi wrap arounds use leftover digit
key = (key % 26);
//check for positive number
if (key < 1)
{
printf("key\n");
return 1; // exit prog
}
// ask for text if pos number
else if (key >= 0)
{
printf("text:");
// check string and read in
fgets(str, sizeof str, stdin);
i = strlen(str);
}
If I type
- ./name (no input, alphabet, two strings returns 1 as it should.)
- ./name (number should work and return 0)
- ./name a1 or b5 return 1 but 1a or 5b return 0 and should not.
In the debugger I can see it only reads the value of the number into argv as 1 or 5 not as 1a or 5b