I am trying to iterate over a string to see if the input is a whole number.
This is the code:
for (int i = 1; i < strlen(buffer); i++) //Checking that each character of the string is numeric
{
if (!isdigit(buffer[i]))
{
valid = false;
break;
}
}
if(valid == false)
{
printf("Invalid input!");
}
else
{
num = atoi(buffer);
printf("The number entered is %d", num);
}
The problem is that even if the input is correct, let us say 2, the output message is still "Invalid input!".
I am very frustrated right now. I have tried using the atoi function, the strtol function and other methods so that I can validate a number and ensure that:
1) it is not a letter 2) it is a whole number
Please help me. I have been trying to solve this problem for over 2 hours.
manpages helps. As well as learning how to use a debugger.int main(void).