Why do my if-else statements keep outputing the same message for valid planets?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • dseals22
    New Member
    • Jan 2017
    • 74

    Why do my if-else statements keep outputing the same message for valid planets?

    I am trying to figure out why my if-else statement won't display the correct message when user types in the valid planet. It insteads outputs the same message for both valid planets and invalid planets inputs. Need help!! Can someone please help me?


    Code:
    #include "stdafx.h"
    #include <iostream> 
    #include <cstdlib> 
    #include <string>
    
     using namespace std;
    
    
    int main() {
        int weight;
        string planet;
        double Mercury_Factor = 0.4155;
        double Venus_Factor = 0.8975;
        double Earth_Factor = 1.0;
        double Mars_Factor = 0.3507;
        double Jupiter_Factor = 2.5374;
        double Saturn_Factor = 1.0677;
        double Uranus_Factor = 0.8947;
        double Neptune_Factor = 1.1794;
        double Pluto_Factor = 0.0899;
        double weightOnPlanet;
    
        cout << "Assignment 5" << endl;
        cout << "The program is written by Daron Seals" << endl;
        cout << "\n";
        cout << "Please Enter Weight (as an integer of pounds): ";
        cin >> weight;
        cout << "Please Enter Planet name (ie: Earth): ";
        cin >> planet;
        cout << "\n";
        cout << "You entered a weight of" << " " << weight << " " << "and a planet name of" << " " << planet << endl;
    
        if (planet == "Mercury")
            weightOnPlanet = weight * Mercury_Factor;
        else
        if (planet == "Venus")
            weightOnPlanet = weight * Venus_Factor;
        else
        if (planet == "Earth")
            weightOnPlanet = weight * Earth_Factor;
        else
        if (planet == "Mars")
            weightOnPlanet = weight * Mars_Factor;
        else
        if (planet == "Jupiter")
            weightOnPlanet = weight * Jupiter_Factor;
        else
        if (planet == "Saturn")
            weightOnPlanet = weight * Saturn_Factor;
        else
        if (planet == "Uranus")
            weightOnPlanet = weight * Uranus_Factor;
        else
        if (planet == "Neptune")
            weightOnPlanet = weight * Neptune_Factor;
        else
        if (planet == "Pluto")
            weightOnPlanet = weight * Pluto_Factor;
    
        if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
            planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
            planet != "Pluto")
            cerr << "Unknown Planet\n";
        cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
    
    
        system("pause");
        return 0;
    }
  • donbock
    Recognized Expert Top Contributor
    • Mar 2008
    • 2427

    #2
    Line 21, initialize weightOnPlanet to -1.0.
    This insures that weightOnPlanet is defined at line 64.

    Replace lines 59-62 with else.
    This eliminates a second set of independent comparisons that might not be precisely consistent with the first set of comparisons.

    Now, tell us precisely what output you get from lines 31, 63, and 64. Be careful to copy uppercase and lowercase letters exactly.

    Comment

    • dseals22
      New Member
      • Jan 2017
      • 74

      #3
      donbock, I asked how can I output the correct message given my last two lines. Nothing else should be changed except the last two statements if that. The message unknown planet it's executed in command prompt when a user inputs a valid planet. It should only be executed when they input an invalid planet. Can you help?
      Last edited by dseals22; Feb 13 '17, 11:54 PM. Reason: Put more information

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Well, let's examine the lines that you are referring to:

        Code:
         if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
                planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
                planet != "Pluto")
                cerr << "Unknown Planet\n";
            cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
        Notice how this line:
        Code:
        cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
        Is not part of an if-else block?

        This means that it will always be executed.

        Since you only want it to be executed if the if conditions fail, you need to add an else!


        So, you simply need to put an else between those lines, everything should work as you want it to because that line of code will only be executed if the first part of the if-block fails:
        Code:
         if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
                planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
                planet != "Pluto")
                    cerr << "Unknown Planet\n";
         else
                    cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
        You should really consider stepping through your code using break points to determine why something isn't working because I'm pretty sure you could have figured this out using this common debugging technique.
        Last edited by Frinavale; Feb 14 '17, 09:20 PM.

        Comment

        • mrijet
          New Member
          • Feb 2015
          • 64

          #5
          This thing should not be discuss in this forum.

          Comment

          • donbock
            Recognized Expert Top Contributor
            • Mar 2008
            • 2427

            #6
            Please provide a precise copy of the program output for a valid planet entry; and then for an invalid planet entry. Please be very careful to precisely and accurately copy the exact output -- especially with respect to upper and lowercase.

            Comment

            Working...