Exit print statement not working.

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 if they type "6" as their new menu choice, it prints another math problem (treats it like the answer was 1), instead of printing the exit statement "Come back soon!". Any help is much 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
// 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 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();
					// How do I make it store the new menu value before going on to the if statement?
					if (menu == 6)
					{
						cout << "Come back soon!" << endl;
					}
					else
					{
						goSwitch(menu);
					}

					return guess;
					return menu;
				}
				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!" << endl;
}
Last edited on
On line 59 you do not take the result from printMenu(), instead you use the value from menu originally passed to goSwitch(...).

Further more: On line 63 goSwitch(...) is called regardless of the menu previously chosen. It should be in the else branch. Again: you do not use the result of that call.

Line 64/65: Two returns. What is it that you want to return?
How do I make it so I am taking the value from printMenu() on line 60?
I suggest that you don't mark the question as solve when it isn't. Changing the original post so that the answer doesn't make sense isn't that good either.

Line 60: menu = printMenu();
Line 67: return goSwitch(menu);
Topic archived. No new replies allowed.