需要'return to main menu' C++代码中的功能



我创建了一个c++程序。在这里,我为用户添加了许多选项。但是在每个选择中,我都需要添加一个功能,用户可以根据自己的选择选择退出程序或返回主菜单。所以我能在编码方面得到帮助吗? xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx-:

#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int choice,p_card;
char text0,text1;
cout<<"nnttt   "Welcome to Zinc hospital"nnn";
cout<<"tMENUn";
cout<<"nn1. Emergency treatmentnn";
cout<<"2. Common treatmentnn";
cout<<"3. Regular checkupsnn";
cout<<"4. Get appointmentnn";
cout<<"5. Consult specialistnn";
cout<<"6. Pay due amountnn";
cout<<"7. Log in for new patient cardnn";
cout<<"8. For suggestions, feedbacks and register complainsnn";
cout<<"nttttttttChoice______";
cin>>choice;
    clrscr();
      if(choice==1)
      {
      int e_choice;
      cout<<"nnn  Enter the type of emergency";
      cout<<"nn1. Accidental case";
      cout<<"nn2. Heavy injury case";
      cout<<"nn3. Delicate organ injurynn";
      cout<<"4. Any othernn";
      cout<<"nntttttttEmergency choice____";
      cin>>e_choice;
      if(e_choice==4)
      {
      cout<<"Please specify the type of emergency ";
      cin>>text0;
      }
      cout<<"nnnnttt      "EMERGENCY DECLARED"nnt  'Please quickly proceed to the operation theatre with patient'";
      }
      if(choice==2)
      {
      cout<<"nnEnter the patient card numbernnt";
      cout<<"tttCard No.______";
      cin>>p_card;
      cout<<"nntttYour card has been recognized succesfully.";
      cout<<"nnnNow enter the specific treatment to be provided___";
      cin>>text0;
      cout<<"nnnn     tttDATA recorded succesfully";
      cout<<"nnYour card has been charged $10.nnnn      Please proceed to counter to get the room no. and wait list serial.";
      }
      else if(choice==3)
      {
      cout<<"";//Under construction
      }
      else if(choice==4)
      {
      cout<<"";//Under construction
      }
      else if(choice==5)
      {
      cout<<"";//Under construction
      }
      else if(choice==6)
      {
      cout<<"";//Under construction
      }
      else if(choice==7)
      {
      cout<<"";//Under construction
      }
      else if(choice==8)
      {
      cout<<"";//Under construction
      }
      else if(choice!=1&&choice!=2&&choice!=3&&choice!=4&&choice!=5&&choice!=6&&choice!=7&&choice!=8)
      {
      cout<<"Invalid choice inputed";
      cout<<"nnnnnntt@Domain ERROR";
      }
getch();
}

查找表(众多解决方案之一)。

typedef (void) (P_Function)(void);
struct Menu_Entry
{
  unsigned int  selection_number;
  const char *  text;
  P_Function    p_processing_function;
};
void Process_Emergency_Treatment(void);
void Process_Common_Treatment(void);
Menu_Entry main_menu[] = 
{
  {1, "Emergency Treatment", Process_Emergency_Treatment},
  {2, "Common Treatment", Process_Common_Treatment},
};
static const unsigned int quantity_menu_items =
  sizeof(main_menu) / sizeof(main_menu[0]);
// ...
unsigned int selection;
std::cout << "Enter selection: ";
std::cin >> selection;
unsigned int index = 0U;
for (index = 0U; index < quantity_menu_items; ++index)
{
  if (main_menu[index].selection_number == selection)
  {
    main_menu[index].p_processing_function(); // Execute the command processor.
    break;
  }
}
if (index >= quantity_menu_items)
{
  std::cout << "nInvalid selection, try again.n";
}

查找表的一个优点是,当您想要向菜单添加项时,可以向表中添加一个条目。此外,您只需要测试搜索循环一次。向表中添加更多条目不会影响搜索循环的执行。

编辑1:基本菜单算法
基本算法如下所示:

bool selection_is_valid = false;
while (!selection_is_valid)
{
  Print_Menu();
  unsigned int selection = 0U;
  std::cout << "Enter selection: ";
  std::cin >> selection;
  if (select >= MAXIMUM_CHOICES)
  {
    selection_is_valid = false;
  }
  else
  {
     Process_Menu_Item(selection);
     selection_is_valid = true;
  }
}  

使用一点技巧,你可以修改上面的算法,直到"存在的选择"被按下。

最新更新