By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,591 Members | 650 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,591 IT Pros & Developers. It's quick & easy.

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

P: 74
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?


Expand|Select|Wrap|Line Numbers
  1. #include "stdafx.h"
  2. #include <iostream> 
  3. #include <cstdlib> 
  4. #include <string>
  5.  
  6.  using namespace std;
  7.  
  8.  
  9. int main() {
  10.     int weight;
  11.     string planet;
  12.     double Mercury_Factor = 0.4155;
  13.     double Venus_Factor = 0.8975;
  14.     double Earth_Factor = 1.0;
  15.     double Mars_Factor = 0.3507;
  16.     double Jupiter_Factor = 2.5374;
  17.     double Saturn_Factor = 1.0677;
  18.     double Uranus_Factor = 0.8947;
  19.     double Neptune_Factor = 1.1794;
  20.     double Pluto_Factor = 0.0899;
  21.     double weightOnPlanet;
  22.  
  23.     cout << "Assignment 5" << endl;
  24.     cout << "The program is written by Daron Seals" << endl;
  25.     cout << "\n";
  26.     cout << "Please Enter Weight (as an integer of pounds): ";
  27.     cin >> weight;
  28.     cout << "Please Enter Planet name (ie: Earth): ";
  29.     cin >> planet;
  30.     cout << "\n";
  31.     cout << "You entered a weight of" << " " << weight << " " << "and a planet name of" << " " << planet << endl;
  32.  
  33.     if (planet == "Mercury")
  34.         weightOnPlanet = weight * Mercury_Factor;
  35.     else
  36.     if (planet == "Venus")
  37.         weightOnPlanet = weight * Venus_Factor;
  38.     else
  39.     if (planet == "Earth")
  40.         weightOnPlanet = weight * Earth_Factor;
  41.     else
  42.     if (planet == "Mars")
  43.         weightOnPlanet = weight * Mars_Factor;
  44.     else
  45.     if (planet == "Jupiter")
  46.         weightOnPlanet = weight * Jupiter_Factor;
  47.     else
  48.     if (planet == "Saturn")
  49.         weightOnPlanet = weight * Saturn_Factor;
  50.     else
  51.     if (planet == "Uranus")
  52.         weightOnPlanet = weight * Uranus_Factor;
  53.     else
  54.     if (planet == "Neptune")
  55.         weightOnPlanet = weight * Neptune_Factor;
  56.     else
  57.     if (planet == "Pluto")
  58.         weightOnPlanet = weight * Pluto_Factor;
  59.  
  60.     if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
  61.         planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
  62.         planet != "Pluto")
  63.         cerr << "Unknown Planet\n";
  64.     cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
  65.  
  66.  
  67.     system("pause");
  68.     return 0;
  69. }
Feb 13 '17 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 2,398
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.
Feb 13 '17 #2

P: 74
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?
Feb 13 '17 #3

Frinavale
Expert Mod 5K+
P: 9,731
Well, let's examine the lines that you are referring to:

Expand|Select|Wrap|Line Numbers
  1.  if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
  2.         planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
  3.         planet != "Pluto")
  4.         cerr << "Unknown Planet\n";
  5.     cout << "On" << " " << planet << " " << "your weight in pounds would be" << " " << weightOnPlanet << endl;
Notice how this line:
Expand|Select|Wrap|Line Numbers
  1. 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:
Expand|Select|Wrap|Line Numbers
  1.  if (planet != "Mercury" && planet != "Venus" && planet != "Earth" && planet != "Mars" &&
  2.         planet != "Jupiter" && planet != "Saturn" && planet != "Uranus" && planet != "Pluto" && planet != "Neptune" &&
  3.         planet != "Pluto")
  4.             cerr << "Unknown Planet\n";
  5.  else
  6.             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.
Feb 14 '17 #4

P: 64
This thing should not be discuss in this forum.
Feb 15 '17 #5

Expert 100+
P: 2,398
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.
Feb 15 '17 #6

Post your reply

Sign in to post your reply or Sign up for a free account.