### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` ``````#include #include #include 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()
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184`` ``````#include #include #include 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.
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185`` ``````#include #include #include 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:

 ``12345678910111213141516171819`` ``````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.