c++ - Functional Loop with user input C++
问题描述
So I got an assignment to make a program that allows the user to select three favourite destinations in order. It repeats until the user decides to stop. Once the user decides to discontinue, the program then displays the total votes received for each destination according to preference by the users. One user will have three preferences and if the program repeats four times, it means four users’ preferences are recorded. Therefore a total of 12 preferences are recorded in this instance.
I have tried to limit input for the loop to work but it seems it will only work with a decision which is not necessary at the beginning of the program, which i want to remove altogether.
Also, I have tried to limit output for each of the decisions but it will only run once and then move on to the next choice. Is there any way to get a persistent entry prompt that will only continue after a valid input.
Lastly, is there any way I could improve the code by using switch/break statements instead of if/else?
Here's my code:
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;
}
解决方案
我要做的是将所有城市变量放入一个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;
}
推荐阅读
- c - 函数“fgetc()”在一段时间内无法正常工作
- codeigniter - 如何将信息从一个模型调用到另一个 Codeigniter
- c# - 如何在不同文本框中的数组中显示所有不同的字符串?
- sql - 具有汇总统计数据的 SQL 查询,按日期分组
- javascript - 使用动态变量映射数据
- python - 如何在 X 轴上制作带有重复标签的图?
- c++ - 我的 QuickSort 代码不适用于 1000000 多个元素(一百万个元素或更多)
- regex - 正则表达式匹配两个字符串之间的所有字符,包括尾部
- c - 为什么 _Generic 检查所有情况的值?
- python - 从 QDockWidget 附加和分离外部应用程序时的问题