How to check the input is integer or not , and only accept integer for input in C

Viewed 27

I have an assignment like : we 'll do a menu and , you 'll get the input < case 1 , 2, 3 > from user to solve each prbs . But there's a question like , if user enter a float or double ,.. in that case how to return like "Enter the value " again when using do .. while ii try lots of ways , but it doesn't work . T.T :<

`

void doMenu(){
 char choice[20] ;
 int i , r, n ;
 r = sscanf(choice,"%d%n",&i,&n);
 bool flag = true;
    printMenu();    
    do{
     do{
        printf("\nInput a number to run its procedure: \n");
        fgets(choice , strlen(choice), stdin );
        if(r== 1 & n== strlen(choice)){
            break;
        }else{
            flag = false ;
        }
     }while(getchar()!= '\n' || flag);
   int  c= (int)(choice -'0');
      flag = true;
    switch(c){
        case 1 : 
            question1();
            break;
        }`
0 Answers
Related