C++循环菜单
C++ looping a menu
我有下面的代码可以编译,但我希望它在用户选择一个选项后循环回原始菜单,以便可以选择另一个选项。
如有任何帮助,我们将不胜感激。
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Select your favourite soft drink:n";
cout<<"Pepsi - 1n";
cout<<"sprite - 2n";
cout<<"fanta - 3n";
cin>>choice;
if(choice==1)
{
cout<<"Good Choice"<<endl;
}
else if(choice==2)
{
cout<<"Not bad"<<endl;
}
else if(choice==3)
{
cout<<"ew!"<<endl;
}
getch();
return 0;
}
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
for(;;){ // <-- loop started here
int choice;
cout<<"Select your favourite soft drink:n";
cout<<"Pepsi - 1n";
cout<<"sprite - 2n";
cout<<"fanta - 3n";
cout<<"exit - 4n"; // <-- break the loop with a 4
cin>>choice;
if(choice==1)
{
cout<<"Good Choice"<<endl;
}
else if(choice==2)
{
cout<<"Not bad"<<endl;
}
else if(choice==3)
{
cout<<"ew!"<<endl;
}
else if(choice==4)
{
break;
}
} // <-- loop end here
getch();
return 0;
}
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int choice;
do{ //Start Loop
cout << "Select your favourite soft drink:n";
cout << "Pepsi - 1n";
cout << "sprite - 2n";
cout << "fanta - 3n";
cin >> choice;
if (choice == 1)
{
cout << "Good Choice" << endl;
}
else if (choice == 2)
{
cout << "Not bad" << endl;
}
else if (choice == 3)
{
cout << "ew!" << endl;
}
} while (choice != 4); // Keep looping until the input is 4.
return 0;
}
相关文章:
- 我正在尝试创建一个菜单,但我的代码一直在循环
- 从子菜单返回后,正确的输入至少进入验证循环一次
- C++ 我的开关格式中的循环不允许我显示菜单选项或接受输入?
- C++循环菜单时做
- 为什么我的 Do-while 循环无法在C++中运行菜单
- 我正在尝试用 c++ 制作菜单,但不明白为什么它不循环
- 我的期末考试遇到了麻烦.代码不断循环回到我的订购菜单,不会显示我的总价
- 试图调节我的 do-while 以停止在终端中退出程序.菜单不会根据需要循环使用当前语句
- 菜单驱动的逻辑错误导致无限循环
- 我的开关菜单在一段时间循环中出现问题
- 为菜单创建基本的用户输入验证(使用基本的 while/do-while 等循环)
- 菜单驱动器输入DO循环
- C++切换语句循环回菜单
- 为项目和循环创建简单的控制菜单会比预期打印提示的次数更多
- 基本菜单驱动程序C ,无限循环
- (C++)如何让它循环回到菜单?计算器类型
- 菜单结构和循环
- 菜单中的C 循环
- C++菜单的“执行”循环.测试大写和小写:
- C++:循环菜单开关