C++-实现一个简单的菜单程序
1,if-else语句实现
实现一个简单的菜单程序,运行时显示"Menu: A(dd) D(delete) S(ort) Q(uit), Select one:"提示用户输入,A表示增加,D表示删除,S表示排序,Q表示退出,输入是A、D、S时分别提示数据已经增加、删除、排序。"输入是Q时程序结束。"
1.1,代码实现
cpp
# include <iostream>
# include <cstdlib>
using namespace std;
int main()
{
char choice, c;
while(1)
{
cout << "Menu: A(dd) D(delete) S(ort) Q(uit), Select one:";
cin >> c;
choice = toupper(c); // 输入字符
if(choice == 'A')
{
cout << "数据已经增加." << endl;
continue;
}
else if(choice == 'D')
{
cout << "数据已经删除." << endl;
continue;
}
else if(choice == 'S')
{
cout << "数据已经排序." << endl;
continue;
}
else if(choice == 'Q')
break;
}
return 0;
}
1.2,功能检测

2,switch语句实现
2.1,代码实现
cpp
#include <iostream>
#include <cstdlib>
using namespace std;
int main()
{
char choice;
while(1)
{
cout << "menu: A(dd) D(elete) S(ort) Q(uit), Select one:";
cin >> choice;
switch(toupper(choice)) // 输入字符,不分大小写
{
case 'A':
cout << "数据已经增加." << endl;
break;
case 'D':
cout << "数据已经减少." << endl;
break;
case 'S':
cout << "数据已经排序." << endl;
break;
case 'Q':
exit(0);
break;
default:
;
}
}
return 0;
}
2.2,功能检测
