mikael
mikael

Reputation: 41

How to Loop this program

so I made this simple program using a tutorial I am following on YouTube, but the thing I wasn't shown is how to give the user the chance to give a new selection if the previous one wasn't valid.

The program is as follows:

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


} 

Upvotes: 0

Views: 75

Answers (3)

tariqebadi
tariqebadi

Reputation: 15

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


}

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 477883

That's a good use case of the do-while loop:

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


} 

The advantage of use a do-while is that the validation is centered on the top and thus compact. The disadvantage of this approach that the validation is centered at the top and you thus more or less duplicate the procedure with the options: if you add a new option, you will have to modify the validation process as well.

Upvotes: 2

Xela
Xela

Reputation: 2392

Need a loop to check if the choice is valid or not if it is valid break the loop, if it is invalid stay in the loop until it is valid.

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


    } 

Upvotes: 2

Related Questions