Assistance with switch case/strings for planet program

Hey guys, I am new to the forum and have a question on my program. The instructions say to write a program that will calculate how much a person would weigh on other planets in our solar system by multiplying their weight by the relative gravity of the chosen planet. I must use a string for the name and planets variable. I am supposed to use parallel arrays to search for the name of the planet entered by the user and find the corresponding relative gravity for that planet from the second array. I also need to use a case structure to allow unique console output statements. This is what I have so far, and when I run the code I get the “Error!” Message. Help would be much appreciated, thanks.

/* Joe McMahon C++ 4
*/

#include <iostream>
#include <iomanip>
#include <string>
#include <algorithm>

using namespace std;

int main()
{
//declare variables and arrays
string planets[8] = { "MERCURY", "VENUS", "EARTH", "MARS", "JUPITER", "SATURN", "URANUS", "NEPTUNE" };
double gravity[8] = { 0.37, 0.78, 1.00, 0.38, 2.64, 1.16, 1.07, 1.21 };
double weight = 0.0;
string planet = "";
string name = "";

//enter name
cout << "Enter your name: (STOP to exit)";
cin >> name;

//begin while
while (name != "STOP")
{
cout << "Enter your weight: ";
cin >> weight;

cout << "What planet to be weighed on? ";
cin >> planet;

//capitalize planet names
transform(planet.begin(), planet.end(), planet.begin(), toupper);

while (weight != -1)
{
for (int x = 0; x < 8; x++)
{
if (planet == planets[x])
{
weight = weight * gravity[x];
//begin switch
switch (x)
{
case 0:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 1:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 2:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 3:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 4:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 5:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 6:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
case 7:
cout << name << " on planet " << planets[x] << " would be " << weight << endl;
break;
default:
cout << " Error! " << endl;
}//end switch
}//end if
}//end for

//ask for additional weights
cout << "Enter your weight: " << endl;
cin >> weight;

cout << "What planet to be weighed on?:(STOP to exit) " << endl;
cin >> planet;

//capitalize planet
transform(planet.begin(), planet.end(), planet.begin(), toupper);
}//end while
}//end while
system("pause");
return 0;
}//end of main
Last edited on
The problem is this:
1
2
3
4
if (planet == planets[x])
code = x;
else // Note: this ends the loop when planet != planets[x]
break;
change it to:
1
2
3
4
5
if (planet == planets[x])
{
code = x;
break;
}
I made your suggestion @coder777 . I still get the errror message when I run the program. I am still unsure on how to fix this. Any other suggestions?
This
1
2
//capitalize planet names
transform(planet.begin(), planet.end(), planet.begin(), toupper);
capitalizes all letters which is not the worst idea, but the array planets does not contain the names with all capital letters.
@coder777 thank you, I fixed that and also managed to change the program around to work!
Topic archived. No new replies allowed.