具有用户输入C++的功能循环

Functional Loop with user input C++

本文关键字:功能 循环 C++ 输入 用户      更新时间:2023-10-16

所以我接到了一个任务,制作一个程序,允许用户按顺序选择三个最喜欢的目的地。它重复,直到用户决定停止。一旦用户决定停止,程序就会根据用户的偏好显示每个目的地收到的总票数。一个用户将有三个首选项,如果程序重复四次,则意味着记录了四个用户的首选项。因此,在这种情况下总共记录了 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 << "nnNow 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;
}

我要做的是将所有城市变量放入一个array中,然后将三组代码转换为for loop。像这样:

for(int i = 0; i < 3; i++) {
if(Choice1 == 0) {
rome[i]++;
}
//etc

这样你就不需要重复相同的代码三次。此外,您只需要一个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;
}

它可以通过使用">Switch"和">Break"来大大简化。

就像我写的代码一样。看一看 :

字符输入;

整数选择;

void Permission(( {

cout << "Do you want to go forth with this program ?                     (y to confirm)" << flush;
cin >> input;
cout << endl;

}

无效 决定1(( {

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;
}

}

无效 决定2(( {

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;

}

无效 决定3(( {

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"一次。如果您知道如何更新此问题,请更新。

是的,你可以用 switch 语句来改进。还有一种叫做 do while 循环的东西,你应该研究一下。