By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,530 Members | 891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,530 IT Pros & Developers. It's quick & easy.

loop

P: 18
hi im new to c what i want is that when the user enters an invalid charartar it tells it runs again and tells to try again my code is
Expand|Select|Wrap|Line Numbers
  1.     a.x = "who won the FIFA world cup 1986";
  2.  
  3.     Expand|Select|Wrap|Line Numbers
  •  
  •     
  •  
  •     printf("%s choose between\nA:Brazil\nB:Italy\nC:Germany\nD:Argentina\n", a.x);
  •     alt = getchar();
  •  
  •  
  •     while (alt = getchar()){
  •  
  •         switch (alt){
  •  
  •         case 'A':
  •             printf("right\n");
  •             break;
  •  
  •         case 'B':
  •             printf("tyvaar\n");
  •             break;
  •         case 'C':
  •             printf("tyvaar\n");
  •             break;
  •         case 'D':
  •             printf("tyvaar\n");
  •             break;
  •         default:
  •  
  •             printf("tyvaar!!! choose right din turk!\n");
  •             break;
  •  
  •         }
  •  
  •         fflush(stdin);
  •  
  •     }
  •     printf("tyvaar!!! choose right din turk!\n");
  •     system("pause");
  • }
  •  
  •     
  •  
  •  
  • Nov 18 '15 #1
    Share this Question
    Share on Google+
    1 Reply


    weaknessforcats
    Expert Mod 5K+
    P: 9,197
    Try this:

    Expand|Select|Wrap|Line Numbers
    1. int main()
    2. {
    3.     while (1)
    4.     {
    5.  
    6.     }  //end of while loop
    7. } //end of main()
    8.  
    This loop will run forever. Now put your loop inside this one:

    Expand|Select|Wrap|Line Numbers
    1. int main()
    2. {
    3.     while (1)
    4.     {
    5.         while (alt = getchar()){
    6.  
    7.             switch (alt){
    8.  
    9.             case 'A':
    10.                 printf("right\n");
    11.                 break;
    12.  
    13.             case 'B':
    14.                 printf("tyvaar\n");
    15.                 break;
    16.             case 'C':
    17.                 printf("tyvaar\n");
    18.                 break;
    19.             case 'D':
    20.                 printf("tyvaar\n");
    21.                 break;
    22.             default:
    23.  
    24.                 printf("tyvaar!!! choose right din turk!\n");
    25.                 break;
    26.  
    27.             }
    28.  
    29.             fflush(stdin);
    30.  
    31.         }
    32.         printf("tyvaar!!! choose right din turk!\n");
    33.         system("pause");
    34.  
    35.  
    36.     }  //end of while (1) loop
    37. } //end of main()
    38.  
    Of course the while (1) loop never stops so you will need to add a case to stop the program. A call to exit(1) will do that.

    You may also need to re-display your menu, etc inside the
    while(1) loop before the inner loop starts.

    Let me know what you think.
    Nov 18 '15 #2

    Post your reply

    Sign in to post your reply or Sign up for a free account.