如何循环此程序

时间:2015-05-27 02:44:19

标签: c++

所以我使用我在 YouTube 上关注的教程制作了这个简单的程序,但我没有展示的是如何让用户有机会给出新的选择。前一个没有效。

该计划如下:

#include <iostream>

using namespace std;

int main() {

    cout << "1. Search" << endl;
    cout << "2. Quit This Program" << endl;
    cout << "3. View Record" << endl;

    cout << "Enter Your Selection Please >   " << flush;

    int input;
    cin >> input;

    switch(input) {
    case 1:
        cout << "Searching..." << endl;
        break;

    case 2:
        cout << "Quitting This Program" << endl;
        break;

    case 3:
        cout << "Searching For Record For Viewing..." << endl;
        break;
    default:
        cout << "That is not a valid option" << endl;
        cout << "Please choose a selection from the menu:" << endl;
    }

    return 0;


} 

3 个答案:

答案 0 :(得分:2)

需要一个循环来检查选择是否有效,如果它是有效的,则打破循环,如果它是无效的,请保持循环直到它有效。

#include <iostream>

    using namespace std;

    int main() {
        bool selection = false;
        while(!selection){
            cout << "1. Search" << endl;
            cout << "2. Quit This Program" << endl;
            cout << "3. View Record" << endl;

            cout << "Enter Your Selection Please >   " << flush;


            int input;
            cin >> input;

            switch(input) {
            case 1:
                cout << "Searching..." << endl;
                selection = true;
                break;

            case 2:
                cout << "Quitting This Program" << endl;
                selection = true;
                break;

            case 3:
                cout << "Searching For Record For Viewing..." << endl;
                selection = true;
                break;
            default:
                cout << "That is not a valid option" << endl;
                cout << "Please choose a selection from the menu:" << endl;
            }
        }

        return 0;


    } 

答案 1 :(得分:2)

这是do - while循环的一个很好的用例:

#include <iostream>

using namespace std;

int main() {

    cout << "1. Search" << endl;
    cout << "2. Quit This Program" << endl;
    cout << "3. View Record" << endl;

    cout << "Enter Your Selection Please >   " << flush;

    int input = -1;
    bool valid = false;
    do {
        cin >> input;
        valid = (cin >= 0 && valid <= 3);
        if(!valid) {
            cout << "Please try again..." << endl;
        }
    } while(!valid);

    switch(input) {
    case 1:
        cout << "Searching..." << endl;
        break;

    case 2:
        cout << "Quitting This Program" << endl;
        break;

    case 3:
        cout << "Searching For Record For Viewing..." << endl;
        break;
    default:
        cout << "That is not a valid option" << endl;
        cout << "Please choose a selection from the menu:" << endl;
    }

    return 0;


} 

使用do - while的好处是验证以顶部为中心,因此紧凑。这种方法的缺点是验证集中在顶部,因此您或多或少地使用选项复制过程:如果添加新选项,则还必须修改验证过程。

答案 2 :(得分:0)

#include <iostream>

using namespace std;

int main() {

    cout << "1. Search" << endl;
    cout << "2. Quit This Program" << endl;
    cout << "3. View Record" << endl;
    do
    {
        cout << "Enter Your Selection Please >   " << flush;

        int input;
        cin >> input;

        switch(input) {
        case 1:
            cout << "Searching..." << endl;
            break;

        case 2:
            cout << "Quitting This Program" << endl;
            break;

        case 3:
            cout << "Searching For Record For Viewing..." << endl;
            break;
        default:
            cout << "That is not a valid option" << endl;
            cout << "Please choose a selection from the menu:" << endl;
        }while(input != 2)

    }

    return 0;


}