Home > Enterprise >  How do I make this repeat?
How do I make this repeat?

Time:10-10

How do I make this repeat the option after the question, for example:


using std::cout;
using std::cin;
using std::endl;

int main()
{
    int Hero = 0, Level = 0, Melee = 0, MeleeRanged = 0, Necklace = 0, Charm = 0, Trinket = 0, ItemTotal = 0;
    int choice;
    int Loop = 0;

    
        cout << "Choose the option you want via the menu below: " << endl;
        cout << "1.) Hero Power " << endl;
        cout << "2.)  " << endl;
        cout << "3.) How does Item Power work? " << endl;
        cout << "4.)  " << endl;
        
        cin >> choice;
        switch (choice)
        {
        case 1:


        {
            cout << "Enter Hero's level: ";
            cin >> Level;

            cout << "Enter Melee's power level: ";
            cin >> Melee;

            cout << "Enter secondary's Melee/Ranged power level: ";
            cin >> MeleeRanged;

            cout << "Enter Necklace's power level: ";
            cin >> Necklace;

            cout << "Enter Charm's power level: ";
            cin >> Charm;

            cout << "Enter Trinket's power level: ";
            cin >> Trinket;

            ItemTotal = Melee   MeleeRanged   Necklace   Charm   Trinket;

            Hero = 10 * Level   ItemTotal / 5;
            
            cout << "Your Hero Power is: " << Hero << endl;
            cout << "Would you like to reselect something in the menu?" << endl;
            cout << "(Y for Yes) (N for No)";
            cin >> choice;
            break;

At the end of Case statement 1, 2, 3, and 4 I want to be able to ask a question, have the user type Y or N, and if Y then repeat the menu before to select a new or the same choice. How can I do this?

CodePudding user response:

You can use a while loop. Assuming that you are familiar with loops. But for menu driven programs, do..while is better:

char ch = 'Y'
do {
    cin >> choice;
    switch (choice)
    {
    case 1: ..... //your choices
    case 2: .....
    case 3: .....
    default:....
    }
    cout << "Do you want to continue?(Y/N)";
    cin >> ch;
} while (ch != 'N');

This menu-driven program will continue until you enter the choice as N.

  •  Tags:  
  • c
  • Related