具有用户输入C++的功能循环
Functional Loop with user input C++
所以我接到了一个任务,制作一个程序,允许用户按顺序选择三个最喜欢的目的地。它重复,直到用户决定停止。一旦用户决定停止,程序就会根据用户的偏好显示每个目的地收到的总票数。一个用户将有三个首选项,如果程序重复四次,则意味着记录了四个用户的首选项。因此,在这种情况下总共记录了 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 循环的东西,你应该研究一下。
相关文章:
- 显示循环双端队列的功能
- 删除功能不适用于串行通信后多个循环中的多个实例
- 具有用户输入C++的功能循环
- 暂停功能,它永远循环
- 运行功能并保持循环运行C
- 如何在 OpenACC 中并行化内部具有"min"功能的循环
- 循环c 时,此POW()在功能中起到了什么作用
- 打电话给功能后,为什么我会陷入无限循环中
- 在 C++ 中直接为 FOR 循环提供的 " ; " 的功能是什么?
- QCoreApplication事件循环和Windows服务控制处理程序功能
- “并非所有控制路径都返回一个值./&quot“控制可能达到非空隙功能的末端”.验证时循环时
- C 迭代用于循环并在其中调用功能
- 如何使特定的部分功能运行并返回主循环
- 在C/C 中使用检查功能的Windows NT命令行代码循环
- 使用循环显示虚拟功能
- 如何准确测量和比较opencl速度以实现循环功能的简单速度
- 将多个功能存储到阵列C 以进行循环
- 循环在C 功能中
- 我的功能跳过C 中的循环参数
- 在 c++e 中使用 do-while 循环进行密码检查功能时出现错误"code will never execute"