How to move back to the main menu ?

I'm writing a menu where the users can choose, any options will make them input a string, after that the program will ask if they want to continue or not, if the users input y then it will continue, otherwise it will return back to the main menu so the users can choose another options.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#include <iostream>
#include <string>
using namespace std;


int main()
{
    string str;
    int optionID;
    char optionContinue;

    cout << "1. Option 1" << endl;
    cout << "2. Option 2" << endl;
    cout << "Choose: ";

    cin >> optionID;

    switch (optionID)
    {
    case 1:
        cout << "You choose option 1 " << endl << endl;
        break;
    case 2:
        cout << "You choose option 2 " << endl << endl;
        break;
    }

    do
    {
        cin.ignore();
        cout << "Enter a string: ";
        getline(cin, str);

        cout << "Continue (y/n): ";
        cin >> optionContinue;

        cout << endl;

    } while (optionContinue == 'y');
}


Until now, I have successfully make the user continue by input y, but I don't know how to make the program return to the main menu if the users input n so they can choose other options. Can you guys help me with this problem ? Thank you
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <iostream>
#include <string>

int menu()
{
    std::cout << "\n1. option 1\n2. option2\n3. quit\n\n" ;

    int opt = 0;
    while( opt < 1 || opt > 3 )
    {
        std::cout << "enter an option (1/2/3): " ;
        std::cin >> opt ; // for now, we assume that this won't fail
    }
    return opt ;
}

bool again()
{
    std::cout << "once again? " ;
    char c ;
    std::cin >> c ;
    return c == 'Y' || c == 'y' ;
}

std::string get_line()
{
    std::cout << "enter a string: " ;
    std::string str ;
    while( str.empty() ) std::getline( std::cin, str ) ; // ignore empty lines
    return str ;
}

void do_option_1()
{
    do
    {
        std::cout << "do option_1\n" ;
        const std::string str = get_line() ;
        // do something with the string, etc. ...
    }
    while( again() ) ;
}

void do_option_2()
{
    do
    {
        std::cout << "do option_2\n" ;
        const std::string str = get_line() ;
        // do something else with the string, etc. ...
    }
    while( again() ) ;
}

int main()
{
    bool quit = false ;
    while( !quit )
    {
        switch( menu() )
        {
            case 1 : do_option_1() ; break ;
            case 2 : do_option_2() ; break ;
            default: quit = true ;
        }
    }

    std::cout << "bye!\n" ;
}
Thank you, I want to ask what is the differrence between the line of code while( str.empty() ) std::getline( std::cin, str ) ; and cin.ignore() ? Which one is better ?
Last edited on
Either should work in this case; but they are somewhat different.

std::cin.ignore( 1'000'000, '\n' ) extracts and discards the rest of the current input line.
If the next line is an empty line, std::getline( std::cin, str ) ; would accept that.

while( str.empty() ) std::getline( std::cin, str ) ; repeats the input till a non-empty line is entered.
If the current line has some extra characters before the new line, it would accept that.

If we want, we can combine the two:
1
2
std::cin.ignore( 1'000'000, '\n' ) ; // extract and discard the rest of the current input line.
while( str.empty() ) std::getline( std::cin, str ) ; // repeat till a non-empty line is entered 
Thank you very much !
Topic archived. No new replies allowed.