uh

enums are arrays are kinda cool :)

Lets take a look-see:

enum planetType {Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, PlanetMax};
double gravities [PlanetMax] = {0.4155, 0.8975, 1.0, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899};

say you decide that there is another planet out there, as is argued by space nerds:
just add its name and g and it fixes itself, no magic number problems:

enum planetType {Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, ThatNewPlanet PlanetMax};
double gravities [PlanetMax] = {0.4155, 0.8975, 1.0, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899, 0.123};

now, you can use the names.
case Mercury:
weightOnPlanet = weight * gravities [Mercury]; //much more readable!

Also, how about some strings?
string planetNames[PlanetMax] = {"Mercury", "Venus" ... }; //you can fill all that in yourself

and something like...
string s;
cin >> s;
if(s == planetNames[Mercury] planet = Mercury
else...
that logic becomes:
planet = planetMax; //lets borrow this as a flag/sentinel for invalid planet!
for(i = Mercury; i < planetMax; i++)
if(s == planetNames[i]) planet = i;
and now...
if planet == planetmax its invalid, show error
else cout << weight*gravities[planet];

unfortunately you need a switch statement, so the above coolness isnt useful (your original code is close enough, you can get it there)...

so basically, you need an array of the planet names as a string, get the name from the user, and convert that back to an enum, then you can switch off it. I think everything you need is above, in rough format.

Use the default: case for the error where the user put in invalid planet, if you want.
The switch statement is WRONG, its bad design, since you can convert it to a single statement, but you have to use it because school. Its doubly wrong because it forces you to convert string to enum -- basically a bunch of extra cpu work to get back to what you already knew -- but the point it to learn to use a switch, nevermind the bad example :P
Last edited on
if you did as I said, if its wrong, it will throw the default switch / case statement and give an error.
switches ONLY work off of integers or integer-like types.
so you need to convert the user input into the enum:
after line 49, you need the for loop I gave you above that does this. It was easy to miss in the wall of text. On top of this, you have planetName as string, not an array of string, so its messed up, and worse, you over-write it with your cin statement.

putting all that together, you have some minor issues to fix, along these lines:

string planetin;
cin >> planetin; //replace cin planetname with something else like this.
//be sure to turn planetName into an array.

THIS PART looks like it should be your find planet name function call
planetType planet = planetMax; //lets borrow this as a flag/sentinel for invalid planet!
for(i = Mercury; i < planetMax; i++)
if(planetin== planetName[i]) planet = i;

switch(planet) //now its working off the enum, which is an integer type...
{
I think you have it right
the smart* in me would say
case Mercury:
case Venus:
case Mars:
//list them all (NO BREAKS) and do the work once:
cout<<"If you currently weigh "<<weight<<" pounds,"<<endl;
cout<<"then you would weigh "<<weight * gravities [planet]<<" "<<"pounds on " planetNames[planet]<<"!"<<endl;
default: ...
}

Last edited on
Actually you have more issues -- the functions do not belong inside of main, and the return statements, while loop, etc have some minor 'not quite rightedness'.

Here, this isnt complete: you need to write the functions your teacher wanted, and so on, but it works and can serve you as a compiling starting point to polish it up and finish.
Ignore the extra includes; its a dump file for code off the forum and it has a bunch of extras.

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
78
#include <string>
#include <set>
#include <vector>
#include <functional>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include<valarray>
#include<fstream>
using namespace std;

enum planetType {Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto, PlanetMax};
const double gravities [PlanetMax] = {0.4155, 0.8975, 1.0, 0.166, 0.3507, 2.5374, 1.0677, 0.8947, 1.1794, 0.0899};
const string planetName[PlanetMax] = {"Mercury", "Venus", "Earth", "Moon", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto"};
 
//double calcWeight(planetType, double); //TBD?

planetType findPlanetName(string &str)
 {
    for(int i = Mercury; i < PlanetMax; i++)
       if(str == planetName[i]) return (planetType)i;
    return PlanetMax; //default
 }
//void GetUserInput(double &UserWeight, string &InputPlanet);  //TBD?

int main()
{  
    
    double weight = 0.0;
    string repeat;
    string planetIn;    
    // Prompt user to enter his/her weight and the name of a planet
    //cout << "Your Name!"<<endl;
    //cout <<""<<endl;
	  while(true)
       {

		cout << "We are going to determine your weight on another planet!"<<endl;
		cout << "You don't have to be honest :)"<<endl;
		cout << " "<<endl;
		cout << "Please Enter Weight: ";
		cin >> weight;   
		cout<< ""<<endl;
		cout << "Enter name of planet exactly as shown!" << endl;
		cout << " "<<endl;
		cout << "Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto" << endl;
		cout <<""<<endl;
		std::cout << "Enter planet name here: ";
        cin >> planetIn;           
         planetType planet = findPlanetName(planetIn);      
                
        switch(planet)
            {
                    
                case Mercury:                    
                case Venus:                    
                case Earth:                    
                case Moon:                   
                case Mars:                    
                case Jupiter:
                case Saturn:                    
                case Uranus:                    
                case Neptune:
                case Pluto:                   
                    
                    cout<<"If you currently weigh "<<weight<<" pounds,"<<endl;
                    cout<<"then you would weigh "<<weight * gravities [planet]<<" "<<" pounds on " << planetName[planet] << "!"<<endl;
                    
                    break;
                    
                default:
                    cout << "Not in our Galaxy. Please check the spelling and try again." << endl;
                    cout << "Please enter, Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Neptune, or Pluto";
			}          
	   }
}
Last edited on
what I posted compiled and ran for me.
It also runs here (edit and run on the side of it in the forum).

1
2
3
4
5
6
7
8
9
10
11
12
13
14
We are going to determine your weight on another planet!
You don't have to be honest :)
 
Please Enter Weight: 11

Enter name of planet exactly as shown!
 
Mercury, Venus, Earth, Moon, Mars, Jupiter, Saturn, Uranus, Neptune, Pluto

Enter planet name here: Mars
If you currently weigh 11 pounds,
then you would weigh 3.8577  pounds on Mars!
We are going to determine your weight on another planet!
You don't have to be honest :)


it probably wants you to set it to version c++17, or you can try cutting my headers and using your old set (I didn't use most of the ones that tagged along with your code).
Last edited on
Topic archived. No new replies allowed.