Hi Issues Code Terminating Entered Choice Either 1 2 3 Last Thing Would Execute Weight Pla Q43876742
Hi, I am having issues with my code. It was terminating afteryou entered your choice either 1,2,3 and the last thing it wouldjust execute was “If your weight on planet Earth……” nothingafter that cout statement
so I wanted to go in and find the solution but when I did tryto, I ran it again and now my program is in an infinite loop. Theprogram is suppose to ask for you weight on “earth” and then if itis in lbs, oz, kg. Then from your choice execute your weight inthat unit in all the planets with the formula for all the plantswhich I wrote in the code. I am still new to coding so I understandif there’s better ways in writing a code like this but I did what Ithought 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 onanother 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): n lbs – 1 n kg – 2 n 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! n”;
wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus
cin >> wtPlanet;
cout << wtPlanet << ” lbs on Venus! n”;
wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter
cin >> wtPlanet;
cout << wtPlanet << ” lbs on Jupiter! n”;
wtPlanet = wtEarth * (0.58 / 9.81); //Pluto
cin >> wtPlanet;
cout << wtPlanet << ” lbs on Pluto! n”;
wtPlanet = wtEarth * (1.62 / 9.81); //Moon
cin >> wtPlanet;
cout << wtPlanet << ” lbs on the moon! n”;
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! n”;
wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus
cin >> wtPlanet;
cout << wtPlanet << ” kg on Venus! n”;
wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter
cin >> wtPlanet;
cout << wtPlanet << ” kg on Jupiter! n”;
wtPlanet = wtEarth * (0.58 / 9.81); //Pluto
cin >> wtPlanet;
cout << wtPlanet << ” kg on Pluto! n”;
wtPlanet = wtEarth * (1.62 / 9.81); //Moon
cin >> wtPlanet;
cout << wtPlanet << ” kg on the moon! n”;
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! n”;
wtPlanet = wtEarth * ( 8.87 / 9.81); //Venus
cin >> wtPlanet;
cout << wtPlanet << ” oz on Venus! n”;
wtPlanet = wtEarth * (24.92 / 9.81); //Jupiter
cin >> wtPlanet;
cout << wtPlanet << ” oz on Jupiter! n”;
wtPlanet = wtEarth * (0.58 / 9.81); //Pluto
cin >> wtPlanet;
cout << wtPlanet << ” oz on Pluto! n”;
wtPlanet = wtEarth * (1.62 / 9.81); //Moon
cin >> wtPlanet;
cout << wtPlanet << ” oz on the moon! n”;
break;
default:
cout << ” invalid input “;
}
cout << ” Would you like to enter another weight (y/n)?”;
cin >> choice;
}
return 0;
}
Expert Answer
Answer to 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…
OR