我有下面的代码编译但是我希望它在用户选择了一个选项之后循环回原始菜单,以便可以选择另一个。
非常感谢任何帮助。
#include <conio.h>
#include <iostream>
using namespace std;
int main()
{
int choice;
cout<<"Select your favourite soft drink:\n";
cout<<"Pepsi - 1\n";
cout<<"sprite - 2\n";
cout<<"fanta - 3\n";
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;
}
答案 0 :(得分: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 - 1\n";
cout<<"sprite - 2\n";
cout<<"fanta - 3\n";
cout<<"exit - 4\n"; // <-- 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;
}
答案 1 :(得分: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 - 1\n";
cout << "sprite - 2\n";
cout << "fanta - 3\n";
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;
}