weight across planets problem

I am trying to write a program that will incorporate enum and the purpose of the program is to take user's input (planetName and weight) and calculate the weight difference between each planet, and then output the result. However, I am having some issues calling my functions. For example, planetNames findPlanetName(string, str) has an error of: "error: 'planetNames' does not name a type." Is this because I need to give a data type to planetNames, and will I also need to give one to findPlanetName? Also, when it references back to the function, I have the same thing written there. I reference a C++ textbook, and from what I see, this is correct. I can not figure out what I need to do. Please help!!!
You ... need ... to ... show ... code ....
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include <iostream>
#include <string>
#include <iomanip>

using namespace std;

// enumerated type declaration to represent planets.
enum Planets {Mercury, Venus, Earth, Moon, Mars, Jupiter,
              Saturn, Uranus, Neptune, Pluto};

// function prototypes
planetNames findPlanetName(string str);
double calcWeight(planetNames, double);

int main()
{
    double weight;
    double weightOnPlanet;
    string planetName;
    // Used to separate the lines cosmetically
    string lineSeparator = "**********************************************************************************";
    string spacer = "                   "; // Used to create space in program cosmetically.

    cout << lineSeparator << endl;
    cout << spacer << "What is your weight on different planets?" << spacer << endl;
    cout << lineSeparator << endl << endl ;

    cout << "Each planet have distinct features. For some of them, they have many rings around them," << endl;
    cout << "and others rain diamonds. While these features are amazing, they are hard to relate to" << endl;
    cout << "since they have little bearing with us here on Earth. However, there is one trait among" << endl;
    cout << "each individual planet that we can relate to, and that is their surface's gravitational" << endl;
    cout << "pull." << endl << endl;

    cout << "While many factors are involved in their gravity, the main characteristic that defines" << endl;
    cout << "it is their respective size compared to the others." << endl;

    cout << "So, what makes this trait so we can relate to it? Well, the gravitational pull has certain" << endl;
    cout << "effects on our bodies as well if we are on the surface, and the most notable one is our" << endl;
    cout << "weight. This program allows you to enter your weight and the name of any planet (including" << endl;
    cout << "the moon) and it will calculate your weight on the planet to show you how much this affects" << endl;
    cout << "you." << endl << endl;

        // Prompt user to enter his/her weight and the name of a planet
    cout << "Please choose from any of these planets:" << endl;
    cout << "Mercury" << endl;
    cout << "Venus" << endl;
    cout << "Earth" << endl;
    cout << "Moon" << endl;
    cout << "Mars" << endl;
    cout << "Jupiter" << endl;
    cout << "Saturn" << endl;
    cout << "Uranus" << endl;
    cout << "Neptune" << endl;
    cout << "Pluto" << endl << endl;
    cout << "Enter planet name here: ";
    cin >> planetName;

    cout << "Enter your weight: ";
    cin >> weight;

    // Call the function findPlanetName to get the name of the planet as enum value. Call the
    // function calcWeight with planet name (enum value) and weight to get the weight on the planet.

    weightOnPlanet = calcWeight (findPlanetName(planetName), weight);

    // Display the planet name, and the weight on that planet
    cout << "Weight on the " << planetName << " is " << weightOnPlanet << endl;

    // pause the system
    system("pause");
    return 0;
}
//*******************************************************************************************
// Functions
//*******************************************************************************************

// The following function uses the string to find and return the planet name in the enumeration
// type that represents the planet names. It displays an error message if the planet is invalid.

// function to find the planet name if the planet name entered by the user is valid, then it
// returns the corresponding enum value

planetNames findPlanetName(string str)
{
     enum planetNames name = Earth;
     // Check if the planet name is valid or not based on length
     if (str.length()<4)
     {
         cout << "Invalid planet name. Please check the spelling and try again." << endl;
         system("pause");
         exit (1);
     }

     // Switch case to find the name of the planet corresponding to the string
     switch (toupper(str.at(0)))
     {
     case 'M':
        switch (toupper(str.at(1)))
        {
        case 'E':
            name = Mercury;
            break;
        case 'A':
            name = Mars;
            break;
        case 'O':
            name = Moon;
            break;
        default:
            cout << "Invalid planet name. Please check the spelling and try again." << endl;
            system("pause");
            exit(1);
        }
        break;
        case 'V':
            name = Venus;
            break;
        case 'E':
            name = Earth;
            break;
        case 'J':
            name = Jupiter;
            break;
        case 'S':
            name = Saturn;
            break;
        case 'U':
            name = Uranus;
            break;
        case 'N':
            name = Neptune;
            break;
        case 'P':
            name = Pluto;
            break;
        default:
            cout << "Invalid planet name. Please check the spelling and try again." << endl;
            system("pause");
            exit(1);
     }
     return name;
}

//*******************************************************************************************
// The following function calculates the weight of the person on the planet of his/her choice.
double calcWeight(planetNames planet, double weight)
{
    double weightOnPlanet = 0;

    // Switch case is used to identify the planet using enum values and calculate the weight
    // on that planet

    switch(planet)
    {

    case Mercury:
        weightOnPlanet = weight * 0.4155;
        break;
    case Venus:
        weightOnPlanet = weight * 0.8975;
        break;
    case Earth:
        weightOnPlanet = weight;
        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 * 0.8947;
        break;
    case Uranus:
        weightOnPlanet = weight * 0.8947;
        break;
    case Neptune:
        weightOnPlanet = weight * 1.1794;
        break;
    case Pluto;
        weightOnPlanet = weight * 0.0899;
        break;
    }
    // Return the weight on that planet
    return weightOnPlanet;
}


error: 'planetNames' does not name a type
error: 'planetNames' was not declared in this scope
error: 'expected primary-expression before 'double'
error: expression list treated as compound expression in initializer [-fpermissive]
In function 'int main()':
error: 'findPlanetName' was not declared in this scope
error: 'calcWeight' cannot be used as a function
error: 'system' was not declared in this scope
error: 'planetNames' does not name a type
error: redefinition of 'double calcWeight'
note: 'double calcWeight' previously defined here
error: 'planetNames' was not declared in this scope
error: expected primary expression before 'double'
its because

double calcWeight(planetNames, double);
---------------------^^^^^^^^

does not have a type that the compiler groks. If planetNames is a user defined type, it does not know that yet.

you either forgot a header file in your #includes, or you did something else. It may be you need a forward declaration (you can search on that term to see one) but most likely it would be a missing header ( I don't see a useful .h file that looks like it would have it in your list).

alternately, you meant this to be your enum, but if that is the case, you maybe meant Planets not planetNames?

I once wrote a compiler with logic like that, using the shortest string and assuming the programmer got the code right. It didnt go well. Here, if I enter planet Stupid, it will work. It works great if the user is being reasonable, of course.
Last edited on
Did you originally name your enum "planetNames"?

You have an enum type named "Planets", but you are trying to use "planetNames".
Okay. Let me try some of those things out. Also, sorry for not posting the code with the original post. For whatever reason, it only works when I enter the code and output in a comment after the thread is made. Not too sure what is going on there.
Changing the enum to planetNames fixed the compiling issues for me. However, there is now an issue with my switch statement directly after the functions section. When I input the correct planets, it goes without a hitch. However, if I am to put "jobs" in for my planet, it treats it as if I put in Jupiter...

I think the issue with it is that I am using toupper throughout that switch statement and it is taking the first letter of the input and using only that. So if, for instance, I put in "garage" for the planet name, it throws my default error message, since there is no case with 'G' in that switch statement.

What would I need to do to get that working correctly, without messing up the rest of the code?
(I only ask because I am using Code::Blocks as my compiler and any change I make, it automatically saves over my previous save. I don't want to mess something up and not be able to fix it.)
Switch statements only work with integer types (int, short, long, char, enum, etc.) So, yes, the fact that you gave it a "J" word, the program interpreted the word as Jupiter.

Since you are working with std::string, you can use toupper on the entire string and then use if ... else if ... else statements to perform your logic.

As far as working with CodeBlocks, just comment out your current code before you write your new code. Then when things are working properly, delete the commented out lines.
its kind of annoying to deal with enums vs text, unfortunately.
you have 2 reasonable choices (and a few variations on those)
convert the user's input to the enum value ... in rushed psuedocode..

1
2
string ptable[] = {"Mercury", "Venus", "Earth", Moon, Mars, Jupiter, //add quotes from the copy from your enum...
              Saturn, Uranus, Neptune, Pluto};

and then do something like

for()
if input == ptable[i] //this isnt ideal, but there are not that many to search so brute force isnt totally bad either.
something = i;
..
switch something

//now you have an integer, that is convertable to your enum, that can be switched upon as you had it. eg you can now say
case Pluto: //enum pluto is converted from i.

or you can recode the switch statement as if statements and do string comparisons directly there.

we need an enhanced enum that ties the text and the integer together, which is easy to write and probably already exists in boost or similar. But I would like to see that as one of the language extensions someday.

it would be nice if you added a max after pluto.
then you can iterate an enum: for i = mercury; i < planets_max; i++)
or you can do it your way with for i = mercury; i <= Pluto; i++)
or use a ranged for loop (I get why these exist but prefer explicit when the range is explicit/fixed) and you can use planets_max as a vector/array initial size constant, etc.
Last edited on
That was the solution I needed! Got it working good now! Thanks to all who contributed! You guys have really helped me out!
Nice!

Ive said it before, but maybe this will help a bit going forward:
switch statements have a unique property; the fall-thru. If you leave a break off a case, it will do the next case too, and sometimes, that makes sense. Not often, but once in a while you have some logic that basically says if its X, do Y, but if its Z, do H and then do Y. A switch can do that slightly more efficiently and cleaner than if blocks, so long as the switch variable is an integer type or can be forced into one.

For just about anything else, switches are usually a poor choice. They are limited (integer only) and they are bug prone (forgot a break, or break where didnt want, etc) and they take up more lines of code to express the idea (case line, break line vs if line, and default case extra lines, because you always have a default, right?) ... Avoid them, unless you have a way to exploit their fall through power in a meaningful way.

Topic archived. No new replies allowed.