I cannot get isdigit
to work. Here is some of my code.
void input(int *array, int size) {
int x;
printf("Give me ten numbers. \n");
for (int i = 0; i < size; i++) {
do {
printf("Array[%d]: ", i);
scanf("%d", &x);
scanf("%*[^\n]");
} while (!isdigit(x));
array[i] = x;
}
}
The aim of this program is to read an integer from the keyboard with scanf
(which is no problem). If the input is not a number, it should repeat the while loop, until the user gives a number. With isdigit
, you should be able to identify if a character is an integer or not. It returns 1 if integer, 0 if not. At least, that should be the case. In this code it always returns 0.