Quick question on a return

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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
// Includes
#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <conio.h>
using namespace std;

// Prototypes
int addition();
int subtraction();
int multiplication ();
int division();
int exit();

// Addition Function
int add()
{
	srand ((unsigned int) time(NULL));
	int num1 = 1 + rand() % 1000;
	int num2 = 1 + rand() % 1000;
	double answer;
	double correct;

	cout << "This is an addition math tutor.\n\n";

	cout << "   " << num1 << endl;
	cout << " + " << num2 << endl;

	cout << "\n\nEnter your answer: ";
	cin >> answer;

	correct = num1 + num2;

	cout << "\n\n";

	if ( answer != correct)
	{
		cout << "You got it wrong." << endl;
		cout << "The correct answer is " << correct << endl;
	}
	else if ( answer == correct)
	{
		cout << "Congratulations, your got it right." << endl;
	}
	_getch();

	return main();
}
// Subtraction Function
int subtraction()
{
	srand ((unsigned int) time(NULL));
	int num1 = 1 + rand() % 1000;
	int num2 = 1 + rand() % 1000;
	double answer;
	double correct;

	cout << "This is an addition math tutor.\n\n";

	cout << "   " << num1 << endl;
	cout << " - " << num2 << endl;

	cout << "\n\nEnter your answer: ";
	cin >> answer;

	correct = num1 - num2;

	cout << "\n\n";

	if ( answer != correct)
	{
		cout << "You got it wrong." << endl;
		cout << "The correct answer is " << correct << endl;
	}
	else if ( answer == correct)
	{
		cout << "Congratulations, your got it right." << endl;
	}
	_getch();

	return main();
}
// Multiplication Function
int multiplication()
{
	srand ((unsigned int) time(NULL));
	int num1 = 1 + rand() % 1000;
	int num2 = 1 + rand() % 1000;
	double answer;
	double correct;

	cout << "This is an addition math tutor.\n\n";

	cout << "   " << num1 << endl;
	cout << " * " << num2 << endl;

	cout << "\n\nEnter your answer: ";
	cin >> answer;

	correct = num1 * num2;

	cout << "\n\n";

	if ( answer != correct)
	{
		cout << "You got it wrong." << endl;
		cout << "The correct answer is " << correct << endl;
	}
	else if ( answer == correct)
	{
		cout << "Congratulations, your got it right." << endl;
	}
	_getch();

	return main();
}
// Division Function
int division()
{
	srand ((unsigned int) time(NULL));
	int num1 = 1 + rand() % 1000;
	int num2 = 1 + rand() % 1000;
	double answer;
	double correct;

	cout << "This is an addition math tutor.\n\n";

	cout << "   " << num1 << endl;
	cout << " / " << num2 << endl;

	cout << "\n\nEnter your answer: ";
	cin >> answer;

	correct = num1 / num2;

	cout << "\n\n";

	if ( answer != correct)
	{
		cout << "You got it wrong." << endl;
		cout << "The correct answer is " << correct << endl;
	}
	else if ( answer == correct)
	{
		cout << "Congratulations, your got it right." << endl;
	}
	_getch();

	return main();
}
// Exit Function
int exit()
{
	cout << "Progam is exiting.";

	_getch();

	return (0);
}
// Main Function
int main()
{
	int choice;

	cout << "This program is a math tutor.\n\n";

	do
	{
		cout << "-----------------------------------" << endl;
		cout << "|				         |" << endl;
		cout << "|				         |" << endl;
		cout << "|	1. Addition	         |" << endl;
		cout << "|	2. Subtraction	         |" << endl;
		cout << "|	3. Multiplication         |" << endl;
		cout << "|	4. Division	         |" << endl;
		cout << "|	5. Exit		         |" << endl;
		cout << "|			       	         |" << endl;
		cout << "|				         |" << endl;
		cout << "-----------------------------------" << endl;

		cout << "\n\nEnter a choice: ";
		cin >> choice;

		while ( choice < 1 || choice > 5)
		{
			cout << "Please enter 1, 2, 3, 4, or 5: ";
			cin >> choice;
		}
		switch (choice)
		{
			case 1: addition(); break;
			case 2: subtraction(); break;
			case 3: multiplication(); break;
			case 4: division(); break;
			case 5: exit(); break;
		}
	} while (choice != 5);
	_getch();

	return 0;
}


How can i make the addition, subtraction, multiplication and division function make a return to main function so the user when done can repick a choice.
Last edited on
You declared the functions such a way that they shall return a value of type int. So each function shall contain a return statement with an expression of type int.

But if to follow the logic of the functions it is better if they wouldl return nothing that is if they have return type void

For example

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
void add()
{
	srand ((unsigned int) time(NULL));
	int num1 = 1 + rand() % 1000;
	int num2 = 1 + rand() % 1000;
	int answer;
	int correct;

	cout << "This is an addition math tutor.\n\n";

	cout << "   " << num1 << endl;
	cout << " + " << num2 << endl;

	cout << "\n\nEnter your answer: ";
	cin >> answer;

	correct = num1 + num2;

	cout << "\n\n";

	if ( answer != correct)
	{
		cout << "You got it wrong." << endl;
		cout << "The correct answer is " << correct << endl;
	}
	else if ( answer == correct)
	{
		cout << "Congratulations, your got it right." << endl;
	}
	_getch();
}
Last edited on
thank you vlad. all done with that program.
actually i do have 1 more question, why is the right side of my box in the main function all messed up, its off alignment. Even when i run the program its off alignment.
Last edited on
It is because you are using TAB symbols instead of spaces.
Here are codes of one of your lines

124 9 9 9 9 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 124

that corresponds to

1
2
		cout << "|				         |" << endl;
As you see in the very beginning of the line there are numbers 9 that correspond to the TAB symbol. 32 is the code of the space. You should use spaces instead of tabs.
Last edited on
oh ok, thank you.
Topic archived. No new replies allowed.