Help with menu driven code

I have to write a menu driven program that allows the user to obtain specified measurements of geometric figures. A loop will be used to allow the user to continue the request until they want to stop. Here is the code that 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
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int Menu()
{
	int choice;
	cout << "1 Compute area of a square" << endl;
	cout << "2 Compute area of a rectangle" << endl;
	cout << "3 Compute hypotenuse of a right triangle" << endl;
	cout << "4 Compute area of a right triangle" << endl;
	cout << "5 Compute area of a isosceles triangle" << endl;
	cout << "6 Compute circumference of a circle" << endl;
	cout << "7 Compute area of a circle" << endl;
	cout << "8 Compute volume of a sphere" << endl;
	cout << "99 to stop program" << endl;
	cout << "Enter your choice" << endl;
		cin >> choice;
	return choice;
}
void Step1()
{
	int option = 1;
	double side;
	double AreaOfSquare;
	AreaOfSquare = side * side;
	while (option !=99)
	{
		
		if(option == 1)
		{
			cout << "Enter the Length of the side " << endl;
			cin >> side;
			cout << "The area is "<< AreaOfSquare << endl;
		}
		else
			if(option == 2);
	}
}
void Step2()
{
	int option = 2;
	double length;
	double width;
	double AreaOfRectangle;
	AreaOfRectangle = length * width;
	while (option !=99)
	{
		if(option == 2)
		{
			cout << "Enter the Length of the rectangle " << endl;
			cin >> length;
			cout << "Enter the Width of the Rectangle " << endl;
			cin >> width;
			cout << "The area is "<< AreaOfRectangle << endl;
		}
		else
			if(option == 3);
	}
}
void Step3()
{
	int option = 3;
	double side1;
	double side2;
	double HypotenuseofRightTriangle;
	HypotenuseofRightTriangle = sqrt(side1 * side1 + side2 * side2);
	while (option !=99)
	{
		if(option == 3)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Hypotenuse is "<< HypotenuseofRightTriangle << endl;
		}
		else
			if(option == 4);
	}
}
void Step4()
{
	int option = 4;
	double side1;
	double side2;
	double AreaofRightTriangle;
	AreaofRightTriangle = side1 * side2 / 2;
	while (option !=99)
	{
		if(option == 4)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Area is "<< AreaofRightTriangle << endl;
		}
		else
			if(option == 5);
	}
}
void Step5()
{
	int option = 5;
	double height;
	double base;
	double AreaofIsoscelesTriangle;
	AreaofIsoscelesTriangle = height * base / 2;
	while (option !=99)
	{
		if(option == 5)
		{
			cout << "Enter Height of the Isosceles Triangle " << endl;
			cin >> height;
			cout << "Enter Base of the Isosceles Triangle " << endl;
			cin >> base;
			cout << "The Area is "<< AreaofIsoscelesTriangle << endl;
		}
		else
			if(option == 6);
	}
}
void Step6()
{
	int option = 6;
	double radius;
	double pi;
	double CircumferenceofaCircle;
	pi = 3.1416;
	CircumferenceofaCircle = 2 * radius * pi;
	while (option !=99)
	{
		if(option == 6)
		{
			cout << "Enter Radius of Circle " << endl;
			cin >> radius;
			cout << "The Circumference is "<< CircumferenceofaCircle << endl;
		}
		else
			if(option == 7);
	}
}
void Step7()
{
	int option = 7;
	double radius;
	double pi;
	double AreaofaCircle;
	pi = 3.1416;
	AreaofaCircle = radius * radius * pi;
	while (option !=99)
	{
		if(option == 7)
		{
			cout << "Enter Radius of the Circle " << endl;
			cin >> radius;
			cout << "The Area is "<< AreaofaCircle << endl;
		}
		else
			if(option == 8);
	}
}
void Step8()
{
	int option = 8;
	double radius;
	double pi;
	double VolumeofaSphere;
	pi = 3.1416;
	VolumeofaSphere = 4 * pi * radius * radius * radius /3;
	while (option !=99)
	{
		if(option == 8)
		{
			cout << "Enter Radius of the sphere " << endl;
			cin >> radius;
			cout << "The volume is "<< VolumeofaSphere << endl;
		}
		else
			if(option == 99);
	}
}


The code will not build, I am getting Error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup and Error LNK1120: 1 unresolved externals.
The message is saying it can't find function main().
To be fair, I can't see main() in this code either.

If there are other files in your program, then main() needs to be in one of them. Otherwise it definitely needs to be here.
Last edited on
OK, I fixed the main() is and the program will build successfully and run, but it does not run properly. When I run the program it displays the options to choose, but once one is selected, the programs ends instead of computing the measurements.
1 Compute area of a square
2 Compute area of a rectangle
3 Compute hypotenuse of a right triangle
4 Compute area of a right triangle
5 Compute area of a isosceles triangle
6 Compute circumference of a circle
7 Compute area of a circle
8 Compute volume of a sphere
99 to stop program

Enter your choice
1
Press any key to continue . . .


Here is my code with the main()
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void main()
{
	int choice;
	cout << "1 Compute area of a square" << endl;
	cout << "2 Compute area of a rectangle" << endl;
	cout << "3 Compute hypotenuse of a right triangle" << endl;
	cout << "4 Compute area of a right triangle" << endl;
	cout << "5 Compute area of a isosceles triangle" << endl;
	cout << "6 Compute circumference of a circle" << endl;
	cout << "7 Compute area of a circle" << endl;
	cout << "8 Compute volume of a sphere" << endl;
	cout << "99 to stop program" << endl;
	cout << endl;
	cout << "Enter your choice" << endl;
	cin >> choice;
}
void Step1()
{
	int option = 1;
	double side;
	double AreaOfSquare;
	AreaOfSquare = side * side;
	while (option !=99)
	{
		
		if(option == 1)
		{
			cout << "Enter the Length of the side " << endl;
			cin >> side;
			cout << "The area is "<< AreaOfSquare << endl;
		}
		else
			if(option == 2);
	}
}
void Step2()
{
	int option = 2;
	double length;
	double width;
	double AreaOfRectangle;
	AreaOfRectangle = length * width;
	while (option !=99)
	{
		if(option == 2)
		{
			cout << "Enter the Length of the rectangle " << endl;
			cin >> length;
			cout << "Enter the Width of the Rectangle " << endl;
			cin >> width;
			cout << "The area is "<< AreaOfRectangle << endl;
		}
		else
			if(option == 3);
	}
}
void Step3()
{
	int option = 3;
	double side1;
	double side2;
	double HypotenuseofRightTriangle;
	HypotenuseofRightTriangle = sqrt(side1 * side1 + side2 * side2);
	while (option !=99)
	{
		if(option == 3)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Hypotenuse is "<< HypotenuseofRightTriangle << endl;
		}
		else
			if(option == 4);
	}
}
void Step4()
{
	int option = 4;
	double side1;
	double side2;
	double AreaofRightTriangle;
	AreaofRightTriangle = side1 * side2 / 2;
	while (option !=99)
	{
		if(option == 4)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Area is "<< AreaofRightTriangle << endl;
		}
		else
			if(option == 5);
	}
}
void Step5()
{
	int option = 5;
	double height;
	double base;
	double AreaofIsoscelesTriangle;
	AreaofIsoscelesTriangle = height * base / 2;
	while (option !=99)
	{
		if(option == 5)
		{
			cout << "Enter Height of the Isosceles Triangle " << endl;
			cin >> height;
			cout << "Enter Base of the Isosceles Triangle " << endl;
			cin >> base;
			cout << "The Area is "<< AreaofIsoscelesTriangle << endl;
		}
		else
			if(option == 6);
	}
}
void Step6()
{
	int option = 6;
	double radius;
	double pi;
	double CircumferenceofaCircle;
	pi = 3.1416;
	CircumferenceofaCircle = 2 * radius * pi;
	while (option !=99)
	{
		if(option == 6)
		{
			cout << "Enter Radius of Circle " << endl;
			cin >> radius;
			cout << "The Circumference is "<< CircumferenceofaCircle << endl;
		}
		else
			if(option == 7);
	}
}
void Step7()
{
	int option = 7;
	double radius;
	double pi;
	double AreaofaCircle;
	pi = 3.1416;
	AreaofaCircle = radius * radius * pi;
	while (option !=99)
	{
		if(option == 7)
		{
			cout << "Enter Radius of the Circle " << endl;
			cin >> radius;
			cout << "The Area is "<< AreaofaCircle << endl;
		}
		else
			if(option == 8);
	}
}
void Step8()
{
	int option = 8;
	double radius;
	double pi;
	double VolumeofaSphere;
	pi = 3.1416;
	VolumeofaSphere = 4 * pi * radius * radius * radius /3;
	while (option !=99)
	{
		if(option == 8)
		{
			cout << "Enter Radius of the sphere " << endl;
			cin >> radius;
			cout << "The volume is "<< VolumeofaSphere << endl;
		}
		else
			if(option == 99);
	}
}
You didn't call any of your Step functions in main...
It does precisely what you specified.
At line 20 the user inputs the value of choice.
At line 21, the program terminates.

If you want something else to happen, you need to add the code to make it so.
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void main()
{
	int choice;
	cout << "1 Compute area of a square" << endl;
	cout << "2 Compute area of a rectangle" << endl;
	cout << "3 Compute hypotenuse of a right triangle" << endl;
	cout << "4 Compute area of a right triangle" << endl;
	cout << "5 Compute area of a isosceles triangle" << endl;
	cout << "6 Compute circumference of a circle" << endl;
	cout << "7 Compute area of a circle" << endl;
	cout << "8 Compute volume of a sphere" << endl;
	cout << "99 to stop program" << endl;
	cout << endl;
	cout << "Enter your choice" << endl;
	cin >> choice;

void Step1();
{
	int option = 1;
	double side;
	double AreaOfSquare;
	AreaOfSquare = side * side;
	while (option !=99)
	{

		if(option == 1)
		{
			cout << "Enter the Length of the side " << endl;
			cin >> side;
			cout << "The area is "<< AreaOfSquare << endl;
		}
		else
			if(option == 2);
	}
}
void Step2();
{
	int option = 2;
	double length;
	double width;
	double AreaOfRectangle;
	AreaOfRectangle = length * width;
	while (option !=99)
	{
		if(option == 2)
		{
			cout << "Enter the Length of the rectangle " << endl;
			cin >> length;
			cout << "Enter the Width of the Rectangle " << endl;
			cin >> width;
			cout << "The area is "<< AreaOfRectangle << endl;
		}
		else
			if(option == 3);
	}
}
void Step3();
{
	int option = 3;
	double side1;
	double side2;
	double HypotenuseofRightTriangle;
	HypotenuseofRightTriangle = sqrt(side1 * side1 + side2 * side2);
	while (option !=99)
	{
		if(option == 3)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Hypotenuse is "<< HypotenuseofRightTriangle << endl;
		}
		else
			if(option == 4);
	}
}
void Step4();
{
	int option = 4;
	double side1;
	double side2;
	double AreaofRightTriangle;
	AreaofRightTriangle = side1 * side2 / 2;
	while (option !=99)
	{
		if(option == 4)
		{
			cout << "Enter Side 1 of the Right Triangle " << endl;
			cin >> side1;
			cout << "Enter Side 2 of the Right Triangle " << endl;
			cin >> side2;
			cout << "The Area is "<< AreaofRightTriangle << endl;
		}
		else
			if(option == 5);
	}
}
void Step5();
{
	int option = 5;
	double height;
	double base;
	double AreaofIsoscelesTriangle;
	AreaofIsoscelesTriangle = height * base / 2;
	while (option !=99)
	{
		if(option == 5)
		{
			cout << "Enter Height of the Isosceles Triangle " << endl;
			cin >> height;
			cout << "Enter Base of the Isosceles Triangle " << endl;
			cin >> base;
			cout << "The Area is "<< AreaofIsoscelesTriangle << endl;
		}
		else
			if(option == 6);
	}
}
void Step6();
{
	int option = 6;
	double radius;
	double pi;
	double CircumferenceofaCircle;
	pi = 3.1416;
	CircumferenceofaCircle = 2 * radius * pi;
	while (option !=99)
	{
		if(option == 6)
		{
			cout << "Enter Radius of Circle " << endl;
			cin >> radius;
			cout << "The Circumference is "<< CircumferenceofaCircle << endl;
		}
		else
			if(option == 7);
	}
}
void Step7();
{
	int option = 7;
	double radius;
	double pi;
	double AreaofaCircle;
	pi = 3.1416;
	AreaofaCircle = radius * radius * pi;
	while (option !=99)
	{
		if(option == 7)
		{
			cout << "Enter Radius of the Circle " << endl;
			cin >> radius;
			cout << "The Area is "<< AreaofaCircle << endl;
		}
		else
			if(option == 8);
	}
}
void Step8();
{
	int option = 8;
	double radius;
	double pi;
	double VolumeofaSphere;
	pi = 3.1416;
	VolumeofaSphere = 4 * pi * radius * radius * radius /3;
	while (option !=99)
	{
		if(option == 8)
		{
			cout << "Enter Radius of the sphere " << endl;
			cin >> radius;
			cout << "The volume is "<< VolumeofaSphere << endl;
		}
		else
			if(option == 99);
	}
}
}


I think I fixed the code for the issue above. NOW *facepalm* I'm having a
Run-Time Check Failure #3 - The variable 'side' is being used without being initialized.
. I can't figure this out.
*sigh* It sounds like you are having difficulty with simple code structure. Are you using a book to learn or a website? Either way, reading the early chapters on general program structure will go a long way to help you, plus the section on basic functions and especially how to call them, and how to use their return values.

Just because you declared the functions doesn't mean they'll automatically work. You have to specifically call each one in the proper way with the proper arguments (if any) and assign the return values (again, if any).

Also, just browsing over some of your functions says they are improperly written. To note:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void Step7();
{
	int option = 7;
	double radius;
	double pi;
	double AreaofaCircle;
	pi = 3.1416;
	AreaofaCircle = radius * radius * pi;
	while (option !=99)
	{
		if(option == 7)
		{
			cout << "Enter Radius of the Circle " << endl;
			cin >> radius;
			cout << "The Area is "<< AreaofaCircle << endl;
		}
		else
			if(option == 8);
	}


First, int option = 7. Why is this here? what do you use it for? The test in the IF loop later is completely useless since you have already set option to 7, it HAS to be 7 any time you call this function. There nothing between the assignment and the IF loop to change it.

Second, you declare double radius and double pi, but don't initialize them. Then you try to use them in a statement. That will not work at all. Variables must be initialized before you do anything to them (at least at run time if not during compiling). You DO input the radius later, but pi is still undefined. What you could do is declare pi as a const double at the very beginning of the code (before main() ) that way you don't have to declare or initialize it anywhere else. Also, since you are relying on user input for radius you must put the AreaofaCircle = radius * radius * pi AFTER the input, otherwise how is the program to know what the value of radius is? Edit: OOPS, I just saw that you did indeed initialize PI. My bad. :) You still need to actual calculation statement to after the con >> radius line though so that radius contains the inputted value BEFORE the calculation.

And lastly, like I said in the beginning, the while and if loops are completely unnecessary. I'm assuming what you are testing here is for the exit condition of choice = 99. That testing should be done before you even try to call any of the functions, not in each function itself. Additionally, you don't even use the right variable for it. While it COULD work with a 2nd variable, you'd have to pass choice to the function as an argument which would look like void Step8 (int option) in your function declaration. Then your function call to step 8 would be Step8(choice). This would pass the value contained in CHOICE to the Step8() and it would be assigned to OPTION. But, like I said, this probably won't do what you hope it would (which is exit your program....) A better option would be to use a switch with one of the cases being case 99 : return 0; (at least, I think that would terminate the program... if not someone else will prolly correct me. You might have to use a break to get out of the switch first though... hmmm... don't think so though)

Anyway, like I said, there are some incredibly fundamental errors in this code that really warrant a complete reread of basic C++ programming structure. I'm actually really confused how you got to writing functions already. At least, the book I'm using hasn't even really introduced functions yet, though it does use a few in some examples and I'm familiar with them from previous attempts at learning languages (mostly scripting stuff, but I've tried C before and bleh...)
Last edited on
Topic archived. No new replies allowed.