Issue with menu

I am writing a program that starts by printing a menu. The user then picks 1-6 from the options. If they pick 1, it should print an addition problem using two random integers. If they answer "-1" to the problem, the main menu should reprint, giving them the option to choose a different menu option. I have it working almost perfectly, except when they type "-1" to bring the menu back up, it appears as expected... BUT when they enter their new menu choice, the program ends, instead of bringing them to the menu choice they selected. Any help would be very appreciated!

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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Unit6 better.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include<iostream>
#include<string>
using namespace std;

int printMenu();
int goSwitch(int);

int main()
{
	// Declare variables
	int numOne = (rand() % 9) + 1;
	int numTwo = (rand() % 9) + 1;
	int menu = printMenu();
	int guess = goSwitch(menu);




    return 0;
}
int printMenu()
{
	int guess = -1;
	int menu = 0;
	if (guess == -1)
		cout << "Welcome To The Aritmetic Quiz" << endl;
		cout << "Press 1 for Addition" << endl;
		cout << "Press 2 for Subtraction" << endl;
		cout << "Press 3 for Multiplication" << endl;
		cout << "Press 4 for Division" << endl;
		cout << "Press 5 for Modulus" << endl;
		cout << "Press 6 to Exit" << endl;
		cin >> menu;
		return menu;
}
int goSwitch(int menu)
{
	string operation;
	int answer = 0;
	int guess = 0;
	int numOne = (rand() % 9) + 1;
	int numTwo = (rand() % 9) + 1;
	if (menu != 6)
	{
		switch (menu)
		{
		case 1:
			operation = "plus";
			while (guess == answer)
			{
				answer = numOne + numTwo;
				cout << "What is: " << " " << numOne << " " << operation << " " << numTwo << "?";
				cin >> guess;

				if (guess == -1)
				{
					printMenu();
					return guess;
				}
				else if (guess != -1 and guess != answer)
				{
					cout << "Try again!" << endl;
					guess = 0;
					answer = 0;
				}
				else
				{
					cout << "Correct!" << endl;
					numOne = (rand() % 9) + 1;
					numTwo = (rand() % 9) + 1;
					guess = 0;
					answer = 0;
				}
			}
			return guess;
			break;
		}
	}
	else
		cout << "Come back soon!";
}	












		
Why would it not finish?

When the function goSwitch returns, why would main not finish?
after line 29, add {} until line 37
I think I found an easy solution, but I have a question.

Is it wrong to call main() in other parts of your code? Could this cause issues or it is the same as calling any other function? If I simply adjust the if statement from:


if (guess == -1)
{
printMenu();
}

to:

if (guess == -1)
{
main();
}

it works perfectly! Is this a good solution or is it bad habit to call main?
Is it wrong to call main() in other parts of your code?


Yes. The C++ standard says not to do it. It is wrong.

You need to learn about loops. You're trying to loop. You're trying to do this:

1
2
3
4
5
while (user hasn't said to stop)
{
  show menu
  do things
} 


So learn about loops.

http://www.cplusplus.com/doc/oldtutorial/control/
Topic archived. No new replies allowed.