需要返回主菜单'我的C ++代码中的功能

时间:2016-09-03 18:10:15

标签: c++

我创建了一个C ++程序。在这里,我为用户添加了许多选项。 但是在每个选择中我都需要添加一个函数,用户可以从中选择退出程序,也可以根据自己的选择返回主菜单。我可以在我的编码中获得帮助.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx - :

#include<iostream.h>
#include<conio.h>
void main()
clrscr();
int choice,p_card;
char text0,text1;
cout<<"\n\n\t\t\t   \"Welcome to Zinc hospital\"\n\n\n";
cout<<"\tMENU\n";
cout<<"\n\n1. Emergency treatment\n\n";
cout<<"2. Common treatment\n\n";
cout<<"3. Regular checkups\n\n";
cout<<"4. Get appointment\n\n";
cout<<"5. Consult specialist\n\n";
cout<<"6. Pay due amount\n\n";
cout<<"7. Log in for new patient card\n\n";
cout<<"8. For suggestions, feedbacks and register complains\n\n";
cout<<"\n\t\t\t\t\t\t\t\tChoice______";
cin>>choice;
    clrscr();
      if(choice==1)
      {
      int e_choice;
      cout<<"\n\n\n  Enter the type of emergency";
      cout<<"\n\n1. Accidental case";
      cout<<"\n\n2. Heavy injury case";
      cout<<"\n\n3. Delicate organ injury\n\n";
      cout<<"4. Any other\n\n";
      cout<<"\n\n\t\t\t\t\t\t\tEmergency choice____";
      cin>>e_choice;
      if(e_choice==4)
      {
      cout<<"Please specify the type of emergency ";
      cin>>text0;
      }
      cout<<"\n\n\n\n\t\t\t      \"EMERGENCY DECLARED\"\n\n\t  \'Please quickly proceed to the operation theatre with patient\'";
      }
      if(choice==2)
      {
      cout<<"\n\nEnter the patient card number\n\n\t";
      cout<<"\t\t\tCard No.______";
      cin>>p_card;
      cout<<"\n\n\t\t\tYour card has been recognized succesfully.";
      cout<<"\n\n\nNow enter the specific treatment to be provided___";
      cin>>text0;
      cout<<"\n\n\n\n     \t\t\tDATA recorded succesfully";
      cout<<"\n\nYour card has been charged $10.\n\n\n\n      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<<"\n\n\n\n\n\n\t\t@Domain ERROR";
      }
getch();
}

1 个答案:

答案 0 :(得分:1)

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

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

只需一点技巧,您就可以修改上述算法,直到按下“存在选择”为止。