C++-实现一个简单的菜单程序
1,if-else语句实现
1.1,代码实现
# 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,代码实现
#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;
}