简体   繁体   中英

Getting switch to loop in C++

I am trying to complete an assignment for my C++ class and I am really stuck. I am not sure if I am approaching the problem correctly. I am trying to make a menu based program where it will add items to the 'cart' based on the user's menu selection and will allow the user to increase how many tickets they have in their 'cart'.

Then if they press N, it will go back to the main menu and only when they press 5 to 'checkout' does it give them the total of their order.

Any help would be really appreciated.

Thank you

#include <iostream>

using namespace std;

int main()
{
    char menuOpt;
    int total = 0;
    char moreTix;

    cout << "Use menu options 1-5 to add a ticket to your cart." << endl;
    cout << "Option 1: Adult - Airboat ride & alligator wrestling show.\n";
                       cout << "\t  Price: $10" << endl;
    cout << "Option 2: Adult - Airboat ride, alligator wrestling show & photo with wrestler.\n";
                       cout << "\t  Price: $15" << endl;
    cout << "Option 3: Senior - Airboat ride & alligator wrestling show.\n";
                       cout << "\t  Price: $7" << endl;
    cout << "Option 4: Child - Airboat ride, alligator wrestling show & stuffed toy alligator.\n";
                       cout << "\t  Price: $10" << endl;
    cout << "Option 5: Checkout"
    cout << "Option: ";
        cin >> menuOpt;

    do{
        switch(menuOpt){
        case '1': cout << "Adult airboat ride & alligator wrestling show has been added to your cart." << endl;
                    total += 10;
                    break;
        case '2': cout << "Adult airboat ride, alligator wrestling show & photo with wrestler has been added to your cart." << endl;
                    total += 15;
                    break;
        case '3': cout << "Senior airboat ride & alligator wrestling show has been added to your cart." << endl;
                    total += 7;
                    break;
        case '4': cout << "Child airboat ride, alligator wrestling show & stuffed toy alligator has been added to your cart." << endl;
                    total +=10;
                    break;
        case '5': cout << "The total for your purchase is $" << total << endl;
        default: cout << "You have made an invalid choice. Please make another selection." << endl;
        }
        cout << "Would you like to purchase another ticket? (Y/N) ";
            cin >> moreTix;
    }while (moreTix == 'y' || moreTix == 'Y');

    return 0;
}

If I understand your question correctly, you are not able to see the menu once the user chooses to purchase more tickets.

Put your do statement above this line and it should work:

cout << "Use menu options 1-5 to add a ticket to your cart." << endl;  

Change your code thus:

#include<iostream>

using namespace std

int main() 
{
   char menuOpt;
   int total = 0;
   char moreTix;

   do {

   <the rest of the code remains the same>
   .......

   }while (moreTix == 'y' || moreTix == 'Y');

return 0;
}
#include<iostream>

using namespace std

int main() 
{
   // data members

   do {

   //Your complete code
   .......

   }while (moreTix == 'y' || moreTix == 'Y');

return 0;
}

Whenever a menu-driven has to be made, when user condition to exit the loop is placed, loop always start before adding the menu option.

And also you forgot break keyword after case '5':, Hence it will always show total purchase and also run the default case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM