473,729 Members | 2,335 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Need help with menu program

namcintosh
67 New Member
Hello, everyone.

Well, let me cut to the chase and explain my problem.

I am trying to devise a menu plan that uses the if/else if and the while loop. The program calculates the user's weight on a given planet. It first asks for the user's weight, asks them for a choice. If they choose choices 1 though 9, the user's weight will be calculated on whatever choice they picked (for example, if the user inputs 150 as their weight and choose choice #2 for Venus, then the user's weight on Venus would be 135 pounds. However, I am running into some problems.

When I compile the program, the output does not come out as I expect.
The program and the results are listed below:

Program:
// This menu-driven program uses an if/else statement to calculate
// the user's weight on a given planet. It also inserts a loop.

#include <iostream>
#include <iomanip> //Needed for the setprecison command
#include <conio> //Needed to show black output screen
using namespace std;

int main()
{
int choice;
double weight1, weight2;


// Displays the menu choices on the screen.
cout << "\t\tPlanet Menu\n\n";
cout << "1. Mercury\n";
cout << "2. Venus\n";
cout << "3. Earth\n";
cout << "4. Mars\n";
cout << "5. Jupiter\n";
cout << "6. Saturn\n";
cout << "7. Uranus\n";
cout << "8. Neptune\n";
cout << "9. Pluto\n";
cout << "99. Exit\n\n";

cout << "Enter your weight: ";
cin >> weight1;

while (choice !=99)

if (choice == 1)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mercury." <<endl;
}
else if (choice == 2)
{
cout << "Please make a selection. ";
cin >> choice;
weight2 = weight1 * 0.90;
cout << "You will weight" << weight2 << "on Venus." <<endl;
}
else if (choice == 3)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.00;
cout << "You will weight" << weight2 << "on Earth." <<endl;
}
else if (choice == 4)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mars." <<endl;
}
else if (choice == 5)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 2.50;
cout << "You will weight" << weight2 << "on Jupiter . " <<endl;
}
else if (choice == 6)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.10;
cout << "You will weight" << weight2 << "on Saturn." <<endl;
}
else if (choice == 7)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.80;
cout << "You will weight" << weight2 << "on Uranus." <<endl;
}
else if (choice == 8)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 1.2;
cout << "You will weight" << weight2 << "on Neptune. " <<endl;

}
else if (choice == 9)
{
cout << "Please make a selection ";
cin >> choice;
weight2 = weight1 * 0.01;
cout << "You will weight" << weight2 << "on Pluto. " <<endl;
}
//Outputs the following message when user enters number other
//than 1 through 9 or 99 on the screen.
else if (choice !=99)
{
cout << "The valid choices are 1 through 9 or 99 to exit.\n";
cout << "Run the program again and select one of those.\n";
}

getch();
return 0;
}

Output Line:
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 150
Please make a selection 2
You will weight60on Mercury.
Please make a selection. 2
You will weight135on Venus.
Please make a selection. 99
You will weight135on Venus.


Do you see the problem? No matter what choice I key in for the first choice, it always calculates for Mercury. Then, when I put that choice in again, it calculates the correct choice. Also, the choice 99 should exit, not do a calculation. (Note: The errors are in bold.) And, when I key in a value that is not one of the choices, instead of just showing "The valid choices are 1 through 9 or 99 to exit. Run the program again and select one of those," all I get is an infinite loop with this statement.
Could there be something wrong with my loops???
Apr 6 '07
14 3217
ilikepython
844 Recognized Expert Contributor
Well, I got half of the problem right, thanks to you. :-)

However, I am still running into a problem.
Here is my output:
Output 1
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 185
5
Please make a selection 5
You will weight462.5on Jupiter .
6
Please make a selection 6
You will weight203.5on Saturn.

As you see, it first asks me for my weight. Because I have the cin >> choice line, it waits for me to enter a choice. then, once I enter a choice, it then asks me to make a selection. But, when I deleted the cin >> choice, this is the problem that I ran into:

Output 2:
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 185
Please make a selection 1
You will weight74on Mercury.
Please make a selection 2
You will weight74on Mercury.
Please make a selection. 2
You will weight166.5on Venus.
Please make a selection. 99
You will weight166.5on Venus.

Also, when I enter an invalid amout, it shows the infinite loop again. What do I do. The first input is right (it makes the right calculations and executes correctly), but how can I make it stop asking the user for the choice twice and make it ask for it once instead. Do you have C++ on your computer? Maybe you can copy and paste the program and you will see what I mean.

Thanks!!!
Ok, since you already asked the user for choice at the beginning of the while loop you don't need to ask again. So, in your if blocks, delete the "cin >> choice" line and the "cout << "Please enter your selection" << endl;" line.

Expand|Select|Wrap|Line Numbers
  1. if (choice == 1)
  2. {
  3. cout << "Please make a selection ";
  4. cin >> choice;
  5. weight2 = weight1 * 0.40;
  6. cout << "You will weight" << weight2 << "on Mercury." <<endl;
  7. }
  8.  
should become
Expand|Select|Wrap|Line Numbers
  1. if (choice == 1)
  2. {
  3. weight2 = weight1 * 0.40;
  4. cout << "You will weight" << weight2 << "on Mercury." <<endl;
  5. }
  6.  
That should do it
Apr 7 '07 #11
namcintosh
67 New Member
Ok, since you already asked the user for choice at the beginning of the while loop you don't need to ask again. So, in your if blocks, delete the "cin >> choice" line and the "cout << "Please enter your selection" << endl;" line.

Expand|Select|Wrap|Line Numbers
  1. if (choice == 1)
  2. {
  3. cout << "Please make a selection ";
  4. cin >> choice;
  5. weight2 = weight1 * 0.40;
  6. cout << "You will weight" << weight2 << "on Mercury." <<endl;
  7. }
  8.  
should become
Expand|Select|Wrap|Line Numbers
  1. if (choice == 1)
  2. {
  3. weight2 = weight1 * 0.40;
  4. cout << "You will weight" << weight2 << "on Mercury." <<endl;
  5. }
  6.  
That should do it
Actually, I would like to cout <<"Please make a selection."; each time the user gets ready to make a selection.
When I tried to place the please make a selection under the "what is your weight statement, this is what happened:

Program Section
cout << "Enter your weight: ";
cin >> weight1;
cout << "Please make a selection: ";

while (choice !=99)
{
cin >> choice;

if (choice == 1)
{

weight2 = weight1 * 0.40;
cout << "You will weight" << weight2 << "on Mercury." <<endl;
}


B]Output [/b]
Planet Menu

1. Mercury
2. Venus
3. Earth
4. Mars
5. Jupiter
6. Saturn
7. Uranus
8. Neptune
9. Pluto
99. Exit

Enter your weight: 185
Please make a selection: 3
You will weight185on Earth.
6
You will weight203.5on Saturn.
7
You will weight148on Uranus.
8
You will weight222on Neptune.
9
You will weight1.85on Pluto.
99

I am trying to make it look like this:
Enter your weight: 185
Please make a selection: 3
You will weight185on Earth.
Please make a selection: 6
You will weight203.5on Saturn.
Please make a selection: 7
You will weight148on Uranus.
Please make a selection: 8
You will weight222on Neptune.
Please make a selection: 9
You will weight1.85on Pluto.
99

So, do you have any idea how I can make the "Please make a selection" statement loop after each weight is calculated???

Thanks
Apr 7 '07 #12
ilikepython
844 Recognized Expert Contributor
Yea, just put the "cout << Please make a selection << endl;" line right above the "cin >> choice;" line.
like this:
Expand|Select|Wrap|Line Numbers
  1. while (choice is not 99)
  2. {
  3. cout << "Please make a selection" << endl;
  4. cin >> choice;
  5.  
  6.  
  7. // rest of stuff here
  8. }
  9.  
Apr 7 '07 #13
namcintosh
67 New Member
Yea, just put the "cout << Please make a selection << endl;" line right above the "cin >> choice;" line.
like this:
Expand|Select|Wrap|Line Numbers
  1. while (choice is not 99)
  2. {
  3. cout << "Please make a selection" << endl;
  4. cin >> choice;
  5.  
  6.  
  7. // rest of stuff here
  8. }
  9.  


OHMIGOODNESS!!! My program works, THANK GOD!! Thank you, thank you, thank you.

Now, I can finally rest tonight!!! I am definitely puttling you on my buddy list!! (LOL)
Apr 7 '07 #14
ilikepython
844 Recognized Expert Contributor
OHMIGOODNESS!!! My program works, THANK GOD!! Thank you, thank you, thank you.

Now, I can finally rest tonight!!! I am definitely puttling you on my buddy list!! (LOL)
You are very welcome.
Apr 7 '07 #15

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1616
by: Chetna Joshi | last post by:
We have created a Web Application in ASP.NET using VB.NET. We have also created a Web Setup Project for installing the Web application on the target machine. I need help on creating a shortcut to the first page of the application in the target machine's program menu. I have tried using "User's Program Menu". But there is no option to open a web page (start page of the application) using the "User's Program Menu". Does anyone know a solution...
0
920
by: jkendrick75 | last post by:
I am new to vb.net and am using visual studio.net 2003. My problem is coming from a program that i am trying to write. I initially added a new form to the program and changed the project property to make the new form the starting form. when i press F5 to start the program, it still goes to my old form and does not show my new form. Since then, as i wasn't too far into the program, i decided to rewrite the program, creating the form that...
66
5385
by: genestarwing | last post by:
QUESTION: Write a program that opens and read a text file and records how many times each word occurs in the file. Use a binary search tree modified to store both a word and the number of times it occurs. After the program has read the file, it should offer a menu with three choices. the first is to list all the words along with the number of occurences. The second is to let you enter a word, with the program reporting how many times the...
4
2759
by: georges the man | last post by:
hey guys, i ve been posting for the last week trying to understand some stuff about c and reading but unfortunaly i couldnt do this. i have to write the following code. this will be the last time i ask for an entire code or u can give me the outine of what to do and i ll do it by myself. the description is the following: the program will read a text file that contains historical price of a stock. The program will allow users to query...
12
3014
by: adamurbas | last post by:
ya so im pretty much a newb to this whole python thing... its pretty cool but i just started today and im already having trouble. i started to use a tutorial that i found somewhere and i followed the instructions and couldnt get the correct results. heres the code stuff... temperature=input("what is the temperature of the spam?") if temperature>50: print "the salad is properly cooked." else:
2
1206
by: Sekm | last post by:
Hi there i want a basic menu which the user inputs a number to chose from the menu. Thing is that i need to make sure that the input IS an integer. So i have an InputMismatchException catch but when it occurs, the program just keeps outputting the menu over and over?? static Scanner sc = new Scanner(System.in); public static void menu (){ System.out.println("Please enter a number to select from the following:\n\t1. Create new...
1
2099
by: javabeginner123 | last post by:
i have a java prob, and i have to solve it fast, but i'm just getting to know it, so plz help me solve it with full code completed, thanks so much. the prob is to create a monter fight and there is the description: The monsters are of a very strange kind, called "Bigmon". They have some basic characteristics, like attack and defense power, life points, a name, and a bonus factor that is used in special occasions. In this initial phase of the...
0
8917
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8761
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8148
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6022
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4795
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3238
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2680
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2163
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.