需要'return to main menu' C++代码中的功能
Need 'return to main menu' feature in my C++ code
我创建了一个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;
}
}
使用一点技巧,你可以修改上面的算法,直到"存在的选择"被按下。
相关文章:
- 我可以做些什么来消除或最小化这种将提供相同功能和行为的代码重复
- 如何使用 GDB 从功能C++逐步调试到 std::function 用户代码?
- 在大括号内添加语句会更改代码功能?
- c++ Visual Studio 2015 快捷方式,用于从选择代码中生成功能
- 下面的代码是如何工作的?它输出分解的数字并且功能齐全,我只是不明白它是如何做到的
- 如何避免大多数成员功能相同的代码重复
- 如何简化代码并将开关组合成一个功能?
- 维护/维持两个代码集的风险,一个用于 CPU,一个用于 GPU,需要执行非常相似的功能
- 所以我想出了他的代码,但我不确定如何继续删除功能?
- 为什么GCC违反了调用ABS功能的代码,并用简短的参数进行功能
- 如何构建包含CUDA功能和C 模板功能的代码
- 是否有一种方法可以避免在RVALUE和LVALUE参考中创建功能时避免重复的代码
- C //尝试重写一个工作代码以包括我的功能
- 代码仅在2个功能实现中的1个中溢出
- 如何在现有 c++ 代码中以功能方式实现遥测
- 在C/C 中使用检查功能的Windows NT命令行代码循环
- C Linux代码破坏Windows构建,因为缺少标头意味着功能不确定
- 代码由编译器代替,用于成员功能的功能指针
- 此代码中没有匹配功能
- C宏来启用和禁用代码功能