实现一个简单的菜单程序,运行时显示“Menu:A(dd) D(elete) S(ort) Q(uit),Select one:”提示用户输入。A表示增加,D表示删除,S表示排序,Q表示退出。输入为A、D、S时分别提示“数据已经增加、删除、排序。”,输入为Q时程序结束。[作业]要求使用if-else语句进行判断,用break,continue控制程序流程。要求使用switch语句。
时间: 2023-05-15 13:06:13 浏览: 172
以下是实现简单菜单程序的代码,使用了if-else语句进行判断,用break,continue控制程序流程,同时也使用了switch语句:
#include <iostream>
using namespace std;
int main() {
char choice;
while (true) {
cout << "Menu: A(dd) D(elete) S(ort) Q(uit), Select one: ";
cin >> choice;
if (choice == 'A' || choice == 'a') {
cout << "Data has been added." << endl;
} else if (choice == 'D' || choice == 'd') {
cout << "Data has been deleted." << endl;
} else if (choice == 'S' || choice == 's') {
cout << "Data has been sorted." << endl;
} else if (choice == 'Q' || choice == 'q') {
cout << "Program has ended." << endl;
break;
} else {
cout << "Invalid input, please try again." << endl;
continue;
}
}
return 0;
}
阅读全文