Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
708 views
in Technique[技术] by (71.8m points)

while loop - How to go back to menu using case in switch case in C

How to go back to menu using case in switch case without using GOTO command. So this is the sample:

while(1){
  printf("1: Basic Math
2. Intermediate Math
3. Advance Math");
  printf("Enter your choice: ");
  int choice;
  scanf("%d", &choice);

  switch(choice)
    int theChoices;
    case 1:
      printf("1. Add
 2. Subtract
3. Go back to menu");
      scanf("%d", &theChoices);

      switch (theChoices) {
         case 1:
           //calculation ...
         case 2:
           //calculation ...
         case 3:
           // Go Back to menu which is Basic math, Intermediate Math, Advance math
           // ***** I want to know how do i get back to the main menu. *****
    case 2:
    // ....
// ....................

So my question again is how do i get back to the menu using the case 3. When i try using the keyword break, it automatically close the program when i chose case 3. Please help.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use continue; instead. It will jump out of all cases and continue executing code after them, which will make the program return to the first line after while (1). Don't forget to close each case with a break;.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...