Help with Enum and switch statement

Hi all! I'm needing a little help, this program is meant to calculate and output the body weight of users vs their body weight on planets in our solar system.

When I try to use a switch statement for this program, the entire statement is marked as error specifically 'Illegal case error' and 'case label may only be used within a switch' errors .I have tried comparing my statement with others in my textbook but can find no difference. Please tell me how I can fix this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  #include <cctype>
#include <iostream>
#include <cmath>


using namespace std;

double weight;
double planet_weight;
string planet;
double weightonplanet = 0;



enum planet_names{Mercury,Venus,Earth,Moon,Mars,Jupiter,Saturn,Uranus,Neptune,Pluto};

planet_names findweight(string str);


int main()
{
	cout << "********************************************************************" << endl;
	cout << endl;
	cout << "Enter the name of a planet from the following options:" << endl;
	cout << "(Mercury,Venus,Earth,Moon, Mars,Jupiter,Saturn,Uranus,Neptune,Pluto)" << endl;
	cout << endl;
	cout << "********************************************************************" << endl;
	cin >> planet;

	cout << endl;
	cout << endl;
	cout << "Please enter your current weight (ex:205)" << endl;
	cin >> weight;
	cout << endl;

	cout << "You have entered " << planet << "." << endl;
	cout << "You currently weigh " << weight << "lbs." << endl;
	cout << "Your weigh on " << planet << "is " << weightonplanet << " ." << endl;
	
	while (true)
	{
		switch (string planet);
		{
		case Mercury:
			weightonplanet = weight * 0.4155;
			break;
		case Venus:
			weightonplanet = weight * 0.8975;
			break;
		case Earth:
			weightonplanet = weight * 1.0;
			break;
		case Moon:
			weightonplanet = weight * 0.166;
			break;
		case Mars:
			weightonplanet = weight * 0.3507;
			break;
		case Jupiter:
			weightonplanet = weight * 2.5374;
			break;
		case Saturn:
			weightonplanet = weight * 1.0677;
			break;
		case Uranus:
			weightonplanet = weight * 0.8947;
			break;
		case Neptune:
			weightonplanet = weight * 1.1794;
			break;
		case Pluto :
			weightonplanet = weight * 0.899;
			break;
		}
	}

		return weightonplanet;
Your syntax for the switch statement is not correct.
First, remove the semi-colon on line 42.
Second, remove the word "string" from line 42. planet is already an object, so don't attempt to re-declare it.

Sadly, that's the easy part. As far as the language is concerned the enum value Mercury and the string "Mercury" are two totally different things, with no logical connection whatsoever.

In other words: You can't switch based on strings.

You can do an if-else chain, or you need to convert the strings to enums yourself.
For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
using namespace std;

enum planet_names { None, Mercury, Venus, Earth, Moon, Mars /* etc. */ };  /* Breaking: Moon is now a planet */
const string planets[] { "None", "Mercury", "Venus", "Earth", "Moon", "Mars" };

planet_names planet_from_string(const string& planet)
{
    for (int i = 0; i < 5; i++)
    {
        if (planet == planets[i])
            return (planet_names)i;
    } 
    return None; // in case the user types in garbage
}

int main()
{
    string planet;
    cout << "Planet: ";
    cin >> planet;

    double weight = 43.0;

    double weightonplanet;
	switch (planet_from_string(planet))
	{
	case Mercury:
		weightonplanet = weight * 0.4155;
		break;
	case Venus:
		weightonplanet = weight * 0.8975;
		break;
	case Earth:
		weightonplanet = weight * 1.0;
		break;
	case Moon:
		weightonplanet = weight * 0.166;
		break;
	case Mars:
		weightonplanet = weight * 0.3507;
		break;
    default:
        cout << "Error parsing planet\n";
        return 1;
	}
	
	cout << "Weight on planet " << planet << " = " << weightonplanet << '\n';
}
Ganado wrote:
Breaking: Moon is now a planet

…to say nothing of Pluto :)
Topic archived. No new replies allowed.