Question: Hi, I am having issues with my code. It was terminating after you entered your choice either 1,2,3 and the last thing it would just

Hi, I am having issues with my code. It was terminating after you entered your choice either 1,2,3 and the last thing it would just execute was "If your weight on planet Earth......" nothing after that cout statement

so I wanted to go in and find the solution but when I did try to, I ran it again and now my program is in an infinite loop. The program is suppose to ask for you weight on "earth" and then if it is in lbs, oz, kg. Then from your choice execute your weight in that unit in all the planets with the formula for all the plants which I wrote in the code. I am still new to coding so I understand if there's better ways in writing a code like this but I did what I thought might work..

This is my code:

#include

using namespace std;

int main ()

{

double wtEarth;

double wtPlanet;

char choice = 'y';

int units;

cout << " Welcome, this program calculates your weight on another planet! " << endl;

while ( choice == 'y' || choice == 'Y')

{

cout << " Please enter your weight (none negative values): ";

cin >> wtEarth;

if (wtEarth <= 0){

cout << "Please enter a value greater than zero";

}

do

{

cout << " Please enter preferred weight units ( lbs, kg, oz): lbs - 1 kg - 2 oz - 3 ";

cin >> units;

switch (units)

{

case 1 : cout << " If your weight on planet earth is " << wtEarth << " lbs";

wtPlanet = wtEarth * ( 3.70 / 9.81); //mercury

cin >> wtPlanet;

cout << wtPlanet << " lbs on Mercury! ";

wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus

cin >> wtPlanet;

cout << wtPlanet << " lbs on Venus! ";

wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter

cin >> wtPlanet;

cout << wtPlanet << " lbs on Jupiter! ";

wtPlanet = wtEarth * (0.58 / 9.81); //Pluto

cin >> wtPlanet;

cout << wtPlanet << " lbs on Pluto! ";

wtPlanet = wtEarth * (1.62 / 9.81); //Moon

cin >> wtPlanet;

cout << wtPlanet << " lbs on the moon! ";

break;

case 2 : cout << " If your weight on planet earth is " << wtEarth << " kg";

wtPlanet = wtEarth * ( 3.70 / 9.81); //mercury

cin >> wtPlanet;

cout << wtPlanet << " kg on Mercury! ";

wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus

cin >> wtPlanet;

cout << wtPlanet << " kg on Venus! ";

wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter

cin >> wtPlanet;

cout << wtPlanet << " kg on Jupiter! ";

wtPlanet = wtEarth * (0.58 / 9.81); //Pluto

cin >> wtPlanet;

cout << wtPlanet << " kg on Pluto! ";

wtPlanet = wtEarth * (1.62 / 9.81); //Moon

cin >> wtPlanet;

cout << wtPlanet << " kg on the moon! ";

break;

case 3 : cout << " If your weight on planet earth is " << wtEarth << " oz";

wtPlanet = wtEarth * ( 3.70 / 9.81); //mercury

cin >> wtPlanet;

cout << wtPlanet << " oz on Mercury! ";

wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus

cin >> wtPlanet;

cout << wtPlanet << " oz on Venus! ";

wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter

cin >> wtPlanet;

cout << wtPlanet << " oz on Jupiter! ";

wtPlanet = wtEarth * (0.58 / 9.81); //Pluto

cin >> wtPlanet;

cout << wtPlanet << " oz on Pluto! ";

wtPlanet = wtEarth * (1.62 / 9.81); //Moon

cin >> wtPlanet;

cout << wtPlanet << " oz on the moon! ";

break;

default:

cout << " invalid input ";

}

cout << " Would you like to enter another weight (y/n)? ";

cin >> choice;

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!