I'd like to do a simple input validation check to see if the user entered ONLY one integer.
The code I current have is as follows
The program should allow any leading white spaces before the number and any trailing white spaces after the number.
However it should not allow an integer followed by white space followed by another integer which it currently does.
Any help would be appreciated.
The code I current have is as follows
Code:
#include <stdio.h>
int main(void)
{
int array[10] = {0};
int i, rv;
char c1;
for(i = 0; i < 10; i++)
{
printf("Enter an integer: ");
while(scanf("%d%c", &array[i], &c1) != 2 || !isspace(c1))
{
printf("Please enter only one integer: ");
while(getchar() != '\n');
}
}
for(i = 0; i < 10; i++)
printf("%d ", array[i]);
printf("\n");
return 0;
}
The program should allow any leading white spaces before the number and any trailing white spaces after the number.
However it should not allow an integer followed by white space followed by another integer which it currently does.
Any help would be appreciated.