Menu program as a Switch-Case

I tried to write a menu program as a switch-case structure with a separate function for a switch-case structure itself. The outcome for it currently is an undesired one, hence why I'm asking this question.

Code:
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
#include <iostream>

using namespace std;

int menu(int answer);

int main()
{
    int answer;

    cout << "Pick a choice from the list: " << endl;

    cout << "Pick from choices 1, 2, or 3, depending on what you want from the menu.\n";

    answer = menu(answer);
    cin >> answer;
}

int menu(int answer)
{
    do
    {
        switch (answer)
        {
            case 1:
                cout << "I want to run.\n";
                break;

            case 2:
                cout << "I want to walk.\n";
                break;

            case 3:
                cout << "I just want to talk to my friends.";
                break;

            default:
                cout << "Bad choice! Please try again later.\n";
        }
    } while (answer <= 0 || answer > 3);
    return answer;
}


The output I get is one where it's just an infinite loop of "Bad choice! Please try again later.".
Last edited on
Nowhere in your code do you ever actually obtain a menu choice from the user.
Using cin in the main() function won't help?
Walk through your code:

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
int main()
{
    int answer;  // <- 'answer' is uninitialized, so it will contain garbage.  Let's say it contains
                 //      8 for this example

    //  some stuff gets printed to the user
    cout << "Pick a choice from the list: " << endl;

    cout << "Pick from choices 1, 2, or 3, depending on what you want from the menu.\n";

    // you call 'menu' with answer as the param (remember, at this point answer=8/garbage)
    answer = menu(answer);
    
    /* --- code jumps to 'menu' function --- */
    
int menu(int answer)  // <- answer=8/garbage
{
    do
    {
        switch (answer)  // <- switching on answer, which is 8/garbage
        {
            case 1:       // <- answer does not == 1, so this case is skipped
                cout << "I want to run.\n";
                break;

            case 2:         // <- also skipped
                cout << "I want to walk.\n";
                break;

            case 3:         // <- also skipped
                cout << "I just want to talk to my friends.";
                break;

            default:        // <- since 8/garbage does not match any case, we do the default
                cout << "Bad choice! Please try again later.\n";  //<- print this message
        }
    } while (answer <= 0 || answer > 3); // <- since 8 falls outside 0/3, we loop
         // endlessly repeating the switch statement, and endlessly printing that message over, and over
         //  and over and over.  Since the contents of 'answer' never changes, you never break out of this loop 



It isn't until line 16 (after the call to 'menu') that you actually get the input from the user and put it in your answer variable.
try something like

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

char choice;
cout << "Which do you want to do? (1)Run, (2)Walk,(3)Talk enter the number"<< endl;
cin >> choice;
switch(choice)
{
    case '1': cout << "Whatever";

    case '2': cout << "Whatever";

    case '3': cout << "Whatever";
  
    default: cout << "You've chosen neither";
   
}


You have to ask which they want and save that as a variable. Here I was saving their choice as a character, because I'm telling them to enter 1, 2, or 3.
Use '1', '2', or '3' after case because this is what you do for chars.
I have to get it to reprint the menu if the user inputs something that isn't amongst the actual choices in the menu. How do I reprint it in that situation?

Here's what I have so far:

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
70
71
// Osman Zakir
// 2/14/2015
// Jumping into C++ Practice Problem
// Menu Program using Switch-Case

#include <iostream>

using namespace std;

// function prototypes
int menu(int answer);
// These are to make sure there's a separate function call for each menu choice
int run(int answer);
int walk(int answer);
int talk(int answer);

int main()
{
    int answer;

    cout << "Pick a choice from the list;  (1)Run, (2)Walk, (3)Talk: ";
    cin >> answer;

    menu(answer);
}

int menu(int answer)
{
    int attempts = 0;
    while (answer <= 0 || answer > 3)
    {
        switch (answer)
        {
            case 1:
                run(answer);
                break;

            case 2:
                walk(answer);
                break;

            case 3:
                talk(answer);
                break;

            default:
                cout << "Bad choice! Please try again: ";
                cout << "Pick a choice from the list;  (1)Run, (2)Walk, (3)Talk.\n";
                attempts++;
        }
    }
    return answer;
}

int run(int answer)
{
    cout << "Good choice! This will help you burn some calories!\n";
    return answer;
}

int walk(int answer)
{
    cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
    return answer;
}

int talk(int answer)
{
    cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
    return answer;
}


How do I get my attempts variable to actually update the loop so it doesn't loop infinitely?
Last edited on
Try this:

I did take a lot of stuff you had out. but It should be okay with just this. I don't know if you want to output the number count or not but this is what I have.

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
#include <iostream>
#include <conio.h>

using namespace std;

int answer;

void calculations()
{
	


	switch (answer)
	{
	case 1:
		cout << "Good choice! This will help you burn some calories!\n";
		break;

	case 2:
		cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
		break;

	case 3:
		cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
		break;

	default:
		cout << "Bad choice! Please try again: \n";
		break;
	}
}

int main()
{
	bool keepLooping = true;


	while (keepLooping)
	{

		cout << "Pick a choice from the list;  (1)Run, (2)Walk, (3)Talk: ";
		cin >> answer;

		calculations();
		
		if (answer < 1 || answer > 3)
			keepLooping = true;
		else
			keepLooping = false;
		
	}

	
		_getch();
		return answer;
	
}
That's good, thanks.

Either way, though, I'll have to turn all of the cases into their own separate functions anyway because a practice problem in a later chapter pertaining to this program tells us to do that.

Something like:
1
2
3
4
5
6
7
8
9
10
11
switch(answer)
{
    case 1: 
           run(answer);
    case 2: 
           walk(answer);
    case 3:
           talk(answer);
    default:
           cout << "Bad choice! Please try again later.\n";
}


Edit: Aright, I made it this way now.
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
70
71
72
73
74
75
76
77
78
// Osman Zakir
// 2/14/2015
// Jumping into C++ Practice Problem
// Menu Program using Switch-Case

#include <iostream>

using namespace std;

// function prototypes
int menu(int answer);
// These are to make sure there's a separate function call for each menu choice
int run(int answer);
int walk(int answer);
int talk(int answer);

int main()
{
    int answer;
    bool keepLooping = true;

    while (keepLooping)
    {
        cout << "Pick a choice from the list;  (1)Run, (2)Walk, (3)Talk: ";
        cin >> answer;

        menu(answer);

        if (answer < 1 || answer > 3)
        {
            keepLooping = true;
        }
		else
        {
            keepLooping = false;
        }
    }
}

int menu(int answer)
{
    switch (answer)
    {
        case 1:
            run(answer);
            break;

        case 2:
            walk(answer);
            break;

        case 3:
            talk(answer);
            break;

        default:
            cout << "Bad choice! Please try again:" << endl;
    }
    return answer;
}

int run(int answer)
{
    cout << "Good choice! This will help you burn some calories!\n";
    return answer;
}

int walk(int answer)
{
    cout << "Still a good choice, as you'll burn calories slowly, but surely.\n";
    return answer;
}

int talk(int answer)
{
    cout << "We can't have that! You won't burn any calories unless you either run or walk.\n";
    return answer;
}


All of the functions are of type int and I put main() first.
Last edited on
Topic archived. No new replies allowed.