因此,我得到了一个任务,以制作一个程序,该程序允许用户按顺序选择三个喜欢的目的地。重复直到用户决定停止。一旦用户决定中止,程序将根据用户的喜好显示每个目的地收到的总票数。一个用户将具有三个首选项,如果程序重复四次,则意味着将记录四个用户的首选项。因此,在这种情况下,总共记录了12个首选项。
我试图限制循环的输入,但是似乎只能在程序开始时不需要的决定下起作用,我想将其完全删除。
此外,我尝试限制每个决策的输出,但是它将只运行一次,然后继续执行下一个选择。有什么方法可以获取持久输入提示,该输入提示仅在输入有效后才会继续。
最后,有什么办法可以通过使用switch / break语句而不是if / else来改善代码吗?
这是我的代码:
cout << "Do you want to go forth with this program?\nType y to confirm. The
program will exit if anything else is entered: ";
cin >> Decision;
while (Decision=="y")
{
cout << "\n\nNow please enter the code for which your destination corresponds to: " << endl; //first decision
cin >> Choice1;
if (Choice1 == 1)
{
LasVegas1++;
}
else if (Choice1 == 2)
{
Tokyo1++;
}
if (cin.fail())
{
cout << "Please enter a valid choice" << endl;
continue;
}
cout << " \nNow please enter the second code: " << endl; //second decision
cin >> Choice2;
if (Choice2 == 1)
{
LasVegas2++;
}
else if (Choice2 == 2)
{
Tokyo2++;
}
else
{
cout << "\nError! Please enter a valid code as shown above!\n";
cout << "\nNow please enter the second code: ";
cin >> Choice2;
}
cout << " \nNow please enter the third code: " << endl; //third decsion
cin >> Choice3;
if (Choice3 == 1)
{
LasVegas3++;
}
else
{
cout << "\nError! Please enter a valid code as shown above!\n";
cout << "\nNow please enter the third code: ";
cin >> Choice3;
}
cout << " \nDo you wish to select three more destinations? (Y/N): " << endl;
cin >> Decision;
}
答案 0 :(得分:2)
我要做的是将所有城市变量放入array
,然后将三组代码转换为for loop
。像这样:
for(int i = 0; i < 3; i++) {
if(Choice1 == 0) {
rome[i]++;
}
//etc
这样一来,您无需重复相同的代码3次。另外,您只需要一个Choice
变量。 (您可以在循环的每次迭代中将其重置)
此外,您可以实现switch
语句以稍微清理代码:
switch(Choice1) {
case 1:
LasVegas1++;
break;
case 2:
Tokyo1++;
break;
case 3:
London1++;
break;
case 4:
Paris1++;
break;
case 5:
Dubai1++;
break;
case 6:
Mumbai1++;
break;
case 7:
NewYork1++;
break;
case 8:
Sydney1++;
break;
case 9:
Auckland1++;
break;
case 10:
Rome1++;
break;
case 11:
Other1++;
break;
}
答案 1 :(得分:-1)
是的,您可以使用switch语句进行改进。还有一些应该做的事情,叫做do while循环。
答案 2 :(得分:-1)
使用“ 开关”和“ 断开”可以大大简化此操作。
就像我编写的代码一样。看看:
字符输入;
int选择;
void Permission(){
cout << "Do you want to go forth with this program ? (y to confirm)" << flush;
cin >> input;
cout << endl;
}
void Decision1(){
if(input =='y'){
cout << "Now please enter the code for which your destination corresponds to : " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
break;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
break;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
}
void Decision2(){
cout << "Now please enter the second code: " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
break;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
void Decision3(){
cout << "Now please enter the third code: " << flush;
cin >> choice;
switch (choice) {
case 1:
cout << "LasVegas " << endl;
break;
case 2:
cout << "Tokyo " << endl;
break;
case 3:
cout << "London " << endl;
break;
case 4:
cout << "Paris " << endl;
break;
case 5:
cout << "Dubai " << endl;
break;
case 6:
cout << "Mumbai " << endl;
break;
case 7:
cout << "New York " << endl;
case 8:
cout << "Sydney " << endl;
break;
case 9:
cout << "Auckland " << endl;
break;
case 10:
cout << "Rome " << endl;
break;
case 11:
cout << "Other " << endl;
break;
default:
cout << "Invalid option. Enter Again : " << flush;
cin >> choice;
}
cout << endl;
}
int main(){
Permission();
Decision1();
Decision2();
Decision3();
system("PAUSE");
return 0;
}
我不知道当用户输入错误的选项时如何在默认情况下获得循环。我曾经给过一次“ cin ”。如果您知道如何更新此问题。