Hi,
As said in the title, the program needs to accept only integer values.. It should not accept float point numbers or characters or spaces or integers mixed with characters .. I know it has to do something with the while statement but i cant figure it out .. Your help will be greatly appreciated .. thanks in advance :)
#include<stdio.h>
#include<stdlib.h>
int get_int(int *px){
scanf("%d",px);
while (!scanf("%d",px)&& !isspace()&& getchar()!= '\n'){
printf("Please enter an integer value:");
}
}
int main(int argc, const char *argv[]){
int x,i;
printf("Please enter a number:\n");
get_int(&x);
//for (i=0; i <20; i++)
printf("you input the value: %d\n",x);
fflush(stdin);
}
The program should have/achieve the following output:
prompt$ ./a.out
Please enter a number: asdf
please enter an integer value: 12.3
please enter an integer value: 12ab321
please enter an integer value: 12321
You input the value: 12321
prompt$
- thanks..