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???