0

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;
        }`
  • Please don't use the C++ tag if your question is about C. – Mark Ransom Sep 20 '22 at 02:38
  • 1
    Read a whole line of input as a string using `fgets`, and parse the value out with `sscanf` or `strtol`. If you fail to read an integer from that, then output the error and try again. – paddy Sep 20 '22 at 02:39
  • And nobody's going to write the code for you, you have to show what you've tried and what the problems with it were. – Mark Ransom Sep 20 '22 at 02:39
  • Please use auto-indentation, and run your current code theough a beautifier (there are some available in the web where you can copy-paste your code there and back). – hyde Sep 20 '22 at 04:07
  • When asking questions at stack overflow, it is always good idea to provide a [mcve], check that out for any future questions. – hyde Sep 20 '22 at 04:08

0 Answers0