473,513 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

74 New Member
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
5 2103
donbock
2,426 Recognized Expert Top Contributor
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
dseals22
74 New Member
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
9,735 Recognized Expert Moderator Expert
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
mrijet
64 New Member
This thing should not be discuss in this forum.
Feb 15 '17 #5
donbock
2,426 Recognized Expert Top Contributor
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

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

Similar topics

1
1532
by: Daylor | last post by:
hi. im devloping application in vb.net/mc++,the application use a dll that using tcp/udp (local). the problem: the firewall (zone alarm pro) is always alerting me about the same port even when...
2
5328
by: Gordon H. | last post by:
I'm trying to email a HTML link to a HTML file attached in the same message. I DO NOT want to have the attached HTML file displayed in the message, I just want a link in the email to the HTML file...
1
2950
by: 4004 | last post by:
I would like to open a columnar form (so I can see all the details) from a datasheet form (so I can see what is there) but keep the same recordset and current record. I can do the recordset set...
1
1313
by: walker | last post by:
I'm sorry for post same message three times by mistalke! please delete the older two message,thanks!
1
1068
by: kevin | last post by:
Greetings, I am looking for an efficient way to copy an entire folder into a path. I looked all over MSDN and found ways to copy files individually, but i have a folder with lots of sub...
5
2559
by: nickwright | last post by:
I am currently having difficulties trying to get a macro to work. I am designing a system to automatically select some prices, and there are 5 different menus that can be selected. I am using several...
23
25878
by: Rogiecrockett | last post by:
I am new to this site. I have searched and found similar posts but don't think they are answering my question. I have a basic and simple DB. 4,170,000 records each record being 6 fields. File...
5
2123
by: moroccanplaya | last post by:
hi i have i open a url using urllib.request.urlopen then store the source code by using url.read to a variable and i paste the source code into the tkinter text widget but how do you keep the same...
0
8014
dbrewerton
by: dbrewerton | last post by:
Ok, I know there has to be some way to do this. I was hoping to find a JQuery or PHP code that would take pictures in a folder and display them like in this video: ...
0
7260
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7160
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7537
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7525
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5685
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4746
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3233
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
456
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.