首页 > 解决方案 > 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;

    }

标签: c++loops

解决方案


我要做的是将所有城市变量放入一个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;
      }

推荐阅读