C ++ help:循环和切换

时间:2017-10-04 19:13:32

标签: c++ loops if-statement switch-statement control-flow

如果考虑到它是一个开关,我如何进行循环选项1和2?编程新手并难以解决这个问题。任何帮助都会受到赞赏!

#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;
}

4 个答案:

答案 0 :(得分:0)

使用cin循环来涵盖switchbContinue个案例。您可以使用下面的内容,使用一个额外的变量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; } 来决定继续循环的时间:

add_library()

答案 1 :(得分:0)

简单来说,你可以使用这样的东西:

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

答案 2 :(得分:0)

使用while循环创建新变量但重新格式化一些代码,以便在循环重新开始时用户选项继续显示

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;

这将使程序看起来更好用户

答案 3 :(得分:0)

使用do-while循环,当userOption等于3时将停止,因此在代码userOptionuserOption != 3不等于3时将继续循环。

示例代码:

#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;
}

输出:

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!