How to go back to menu using case in switch case in C

孤者浪人 提交于 2019-12-13 11:25:27

问题


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\n2. Intermediate Math\n3. Advance Math");
  printf("Enter your choice: ");
  int choice;
  scanf("%d", &choice);

  switch(choice)
    int theChoices;
    case 1:
      printf("1. Add\n 2. Subtract\n3. 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.


回答1:


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;.



来源:https://stackoverflow.com/questions/37502242/how-to-go-back-to-menu-using-case-in-switch-case-in-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!