An assignment asks us to create a program in C that asks for user input of only numbers, using getchar() and infinite loop with while(1), that stops after typing a non-numeric character.
This is my attempt:
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main(){
while(1){
int x = getchar();
if (isdigit(x) == 0){
break;
};
};
return 0;
}
But when I run the code and type anything, it will stop after the first attempt, no matter what I type.
Can you please find a way to correct the code above?