0

I'm trying to brush up on some simple C++ programs, and this one has some errors and I'm not understanding why. I'll write the errors in the comments above the line

int main(void)
{

bool x = true;
char userInput;
double weight;
string planetName;

while (x == true)
{
                                      //endl; has the error "expected a ;"
    cout << "Please enter your weight:" endl;
    cin >> weight;

    cout << "Please enter a planet name (The moon and pluto count)" endl;
    cin >> planetName;

    for (int i = 0; i < planetName[i] != '\0'; i++)
    {
        planetName[i] = tolower(planetName[i]);
    }

           //planetName has the error "expression must have integral or enum types"
    switch (planetName)
    {
         //mercury has the error "too many characters in character constant"
    case 'mercury' :
        cout << "Your weight is :" << weight * .4155;
    case 'venus' :
        cout << "Your weight is :" << weight * .8975;
    case 'earth' :
        cout << "Your weight is :" << weight * 1;
    case 'mars' :
        cout << "Your weight is :" << weight * .3507;
    case 'jupiter' :
        cout << "Your weight is :" << weight * 2.5374;
    case 'saturn' :
        cout << "Your weight is :" << weight * 1.0677;
    case 'uranus' :
        cout << "Your weight is :" << weight * .8947;
    case 'neptune' :
        cout << "Your weight is :" << weight * 1.1794;
    case 'pluto' :
        cout << "Your weight is :" << weight * .0899;
    case 'moon' :
        cout << "Your weight is :" << weight * .116;
    }





    cout << "Try again?" << endl;
    cout << "Press Y to see your weight on a different planet,or N to exit." << endl;
    cin >> userInput;
                                   // both == signs have "operand types are incompatable"
    if (userInput == "y" || userInput == "Y")
    {
        break;
    }
    else if (userInput == "n" || userInput == "N")
    {
        x = false;
    }

}




PressEnterToContinue();

return 0;
}

If I'm doing something in a less than intelligent way, or you think of a more intelligent way to do any of this, please feel free to say so. I don't really like using while loops, but sometimes I just can't think of anything else quickly for short programs like this.

4
  • 1
    Why is this tagged C? Commented Dec 10, 2013 at 6:11
  • 1
    change ' to ". ' can only contain ONE char.
    – SliceSort
    Commented Dec 10, 2013 at 6:13
  • 2
    If you'd like to do this in a more modern C++ way, you might use a std::map< std::string, double > to map planet names to gravitational ratios.
    – Joe Z
    Commented Dec 10, 2013 at 6:15
  • It's tagged as C, because I wasn't positive that most of this wasn't correct C syntax, and thanks for the map advice, I'll look into it, thanks. Commented Dec 10, 2013 at 6:25

2 Answers 2

1
                                  //endl; has the error "expected a ;"
cout << "Please enter your weight:" endl;

As it says, you put an endl where it expected a ;. The problem is the missing <<.

       //planetName has the error "expression must have integral or enum types"
switch (planetName)

As it says, a switch must have an integral or enum type. Use if instead.

     //mercury has the error "too many characters in character constant"
case 'mercury' :

As it says, a character constant must be a character. Use " for string constants.

                               // both == signs have "operand types are incompatable"
if (userInput == "y" || userInput == "Y")

Since userInput is a char, it cannot be equivalent to a string.

2
  • Very comprehensive, although you forgot to mention the missing break statements.
    – Joe Z
    Commented Dec 10, 2013 at 6:14
  • I don't think he mentioned the break statements since I can't use a switch statement (the way I'm doing it) anyway. Thanks for the comments and answers! Commented Dec 10, 2013 at 6:29
0

In C and C++ switch statements are defined only for integer types, such as int or shot or long or enum or char or their unsigned versions.

Also strings should be enclosed in double quotes.

Not the answer you're looking for? Browse other questions tagged or ask your own question.