I am trying to do the switch case in which if the user chose a number, the user's weight will be multiplied to the factor for the corresponding planet.
Not sure if that makes sense but if I picked 4 and entered my weight as 150. It will say "Your weight on Earth is 150 lbs." but if I pick 1 which is the Sun it will multiply the user weight to 27.072. I just can't figure out what I will put in my switch case and the cases below switch(). Please help.
what you have looks fine, just need a case for each planet.
case '2'
cout otherplanet*dWeightIn
break
.. etc
stop mixing cout and printf. printf is a C function and you should not use it unless you have a compelling reason to do so.
likewise atoi should be stoi on the string, instead.
it can be frustrating to figure out the c++ version if you are used to C, but you can do anything without reverting to C... just dig around in the documentation and you can find a way.
I'm using printf because that's what our prof wants us to use, by the way I have no any experience with coding at all.
This is what I did based on your suggestion, but now I can't get past my first prompt. It keeps saying invalid input no matter how I change the if condition.
int main()
{
// variables
string strTitle = "CALCULATE YOUR WEIGHT IN ANOTHER PLANET\n\n";
string strError = "\nInvalid input.\n\n";
string strPrompt1 =
"Pick a planet according to their respective number on which you want your weight to be calculated in ";
string strPlanet;
int iPlanet;
string strPrompt2 = "What is your weight in pounds? ";
string strWeightOut = "Your weight is %d";
double dWeightIn;
int iWeightIn;
string strSun = "Sun", strMercury = "Mercury", strVenus = "Venus", strEarth = "Earth",
strMoon = "Moon", strMars = "Mars", strJupiter = "Jupiter", strSaturn = "Saturn",
strUranus = "Uranus", strNeptune = "Neptune", strPluto = "Pluto";
double dSun = 27.072;
double dMercury = 0.378;
double dVenus = 0.907;
double dEarth = 1.0;
double dMoon = 0.166;
double dMars = 0.377;
double dJupiter = 2.364;
double dSaturn = 1.064;
double dUranus = 0.889;
double dNeptune = 1.125;
double dPluto = 0.067;
// show title
printf("%s", strTitle.c_str());
// display menu
cout << left << setw(3) << "1";
cout << right << setw(8) << strSun << endl;
cout << left << setw(3) << "2";
cout << right << setw(8) << strMercury << endl;
cout << left << setw(3) << "3";
cout << right << setw(8) << strVenus << endl;
cout << left << setw(3) << "4";
cout << right << setw(8) << strEarth << endl;
cout << left << setw(3) << "5";
cout << right << setw(8) << strMoon << endl;
cout << left << setw(3) << "6";
cout << right << setw(8) << strMars << endl;
cout << left << setw(3) << "7";
cout << right << setw(8) << strJupiter << endl;
cout << left << setw(3) << "8";
cout << right << setw(8) << strSaturn << endl;
cout << left << setw(3) << "9";
cout << right << setw(8) << strUranus << endl;
cout << left << setw(3) << "10";
cout << right << setw(8) << strNeptune << endl;
cout << left << setw(3) << "11";
cout << right << setw(8) << strPluto << endl << endl;
// prompt user for weight
printf("%s", strPrompt2.c_str());
cin >> dWeightIn;
if ( dWeightIn <= 0 )
{
printf("%s", strError.c_str());
system("Pause");
return 0;
}
// switch
switch ( iPlanet )
{
case '1':
cout << "Your weight on the Sun is: " << dWeightIn * dSun << " lbs.";
break;
case '2':
cout << "Your weight on the Mercury is: " << dWeightIn * dMercury << " lbs.";
break;
case '3':
cout << "Your weight on the Venus is: " << dWeightIn * dVenus << " lbs.";
break;
case '4':
cout << "Your weight on the Earth is: " << dWeightIn * dEarth << " lbs.";
break;
case '5':
cout << "Your weight on the Moon is: " << dWeightIn * dMoon << " lbs.";
break;
case '6':
cout << "Your weight on the Mars is: " << dWeightIn * dMars << " lbs.";
break;
case '7':
cout << "Your weight on the Jupiter is: " << dWeightIn * dJupiter << " lbs.";
break;
case '8':
cout << "Your weight on the Saturn is: " << dWeightIn * dSaturn << " lbs.";
break;
case '9':
cout << "Your weight on the Uranus is: " << dWeightIn * dUranus << " lbs.";
break;
case '10':
cout << "Your weight on the Neptune is: " << dWeightIn * dNeptune << " lbs.";
break;
case '11':
cout << "Your weight on the Pluto is: " << dWeightIn * dPluto << " lbs.";
break;