简体   繁体   中英

C++ help: loops and switches

How do I go about looping options 1 and 2 considering it's a switch? New to programming and having difficulties solving this. Any help would be m uch appreciated!

#include <iostream>
using namespace std;

int main()
{
    int userOption;

    cout << "Hello user, what would you like to do?" << endl;
    cout << "Option 1: Display a message" << endl;
    cout << "Option 2: Perform a calculation" << endl;
    cout << "Option 3: Exit program" << endl;
    cin >> userOption;

        switch (userOption)
        {
            char op;
            int num1, num2;

        case 1:
            cout << "Welcome user!" << endl;
            break;
        case 2:
            cout << "Enter + for addition, - for subtraction, * for multiplication or / for division: ";
            cin >> op;
            cout << "Enter two numbers: ";
            cin >> num1 >> num2;
            if (op == '+')
                cout << num1+num2 << endl;
            else if (op == '-')
                cout << num1-num2 << endl;
            else if (op == '*')
                cout << num1*num2 << endl;
            else if (op == '/')
                cout << num1/num2 << endl;
            break;
        case 3:
            cout << "Goodbye!" << endl;
            return 0;
            break;
        default:
            cout << "Please enter valid option!" << endl;

        }
    return 0;
}

Use while loop to cover cin and switch cases. You can use something like below with use of one additional variable bContinue that will decide how long to continue loop:

int main() 
{
    int userOption;

    cout << "Hello user, what would you like to do?" << endl;
    cout << "Option 1: Display a message" << endl;
    cout << "Option 2: Perform a calculation" << endl;
    cout << "Option 3: Exit program" << endl;
    bool bContinue = true;
    while(bContinue)
    {
        cin >> userOption;

        switch (userOption)
        {
            char op;
            int num1, num2;

        case 1:
            cout << "Welcome user!" << endl;
            break;
        case 2:
            cout << "Enter + for addition, - for subtraction, * for multiplication or / for division: ";
            cin >> op;
            cout << "Enter two numbers: ";
            cin >> num1 >> num2;
            if (op == '+')
                cout << num1+num2 << endl;
            else if (op == '-')
                cout << num1-num2 << endl;
            else if (op == '*')
                cout << num1*num2 << endl;
            else if (op == '/')
                cout << num1/num2 << endl;
            break;
        case 3:
            cout << "Goodbye!" << endl;
            bContinue = false;
            break;
        default:
            cout << "Please enter valid option!" << endl;
        }
    }
    return 0;
}

In simplicity, you could use something like this:

unsigned int selection = 25;
// Print menu and input selection
while (selection != 3)
{
 // Do stuff
  // Print menu and input selection
}

Use a while loop with the creation of a new variable but reformat some of the code so the user options keep displaying when the loop starts over

cout << "Hello user, what would you like to do?" << endl;

bool bContinue = true;
while(bContinue)
{
    cout << "Option 1: Display a message" << endl;
    cout << "Option 2: Perform a calculation" << endl;
    cout << "Option 3: Exit program" << endl;

    cin >> userOption;

    switch (userOption)
    {
        char op;
        int num1, num2;

    case 1:
        cout << "Welcome user!" << endl;
        break;
    case 2:
        cout << "Enter + for addition, - for subtraction, * for multiplication or / for division: ";
        cin >> op;
        cout << "Enter two numbers: ";
        cin >> num1 >> num2;
        if (op == '+')
            cout << num1+num2 << endl;
        else if (op == '-')
            cout << num1-num2 << endl;
        else if (op == '*')
            cout << num1*num2 << endl;
        else if (op == '/')
            cout << num1/num2 << endl;
        break;
    case 3:
        cout << "Goodbye!" << endl;
        bContinue = false;
        break;
    default:
        cout << "Please enter valid option!" << endl;
    }
}
return 0;

This will make the program look better to a user

Use a do-while loop, which will stop when the userOption will be equal to 3, thus it will continue looping while userOption is different than 3, in code userOption != 3 .

Example code:

#include <iostream>
using namespace std;

int main()
{
    int userOption;

    cout << "Hello user, what would you like to do?" << endl;
    do {
        cout << "Option 1: Display a message" << endl;
        cout << "Option 2: Perform a calculation" << endl;
        cout << "Option 3: Exit program" << endl;
        cin >> userOption;

        switch (userOption)
        {
            char op;
            int num1, num2;

            case 1:
                cout << "Welcome user!" << endl;
                break;
            case 2:
                cout << "Enter + for addition, - for subtraction, * for multiplication or / for division: ";
                cin >> op;
                cout << "Enter two numbers: ";
                cin >> num1 >> num2;
                if (op == '+')
                cout << num1+num2 << endl;
                else if (op == '-')
                    cout << num1-num2 << endl;
                else if (op == '*')
                    cout << num1*num2 << endl;
                else if (op == '/')
                    cout << num1/num2 << endl;
                break;
            case 3:
                cout << "Goodbye!" << endl;
                break;
            default:
                cout << "Please enter valid option!" << endl;

        }
    } while(userOption != 3);
    return 0;
}

Output:

Hello user, what would you like to do?
Option 1: Display a message
Option 2: Perform a calculation
Option 3: Exit program
2
Enter + for addition, - for subtraction, * for multiplication or / for division: 
+
Enter two numbers:
1
1
2
Option 1: Display a message
Option 2: Perform a calculation
Option 3: Exit program
3
Goodbye!

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