GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 25-Oct-2009, 04:17
saqib_ saqib_ is offline
New Member
 
Join Date: Oct 2009
Posts: 12
saqib_ has a little shameless behaviour in the past

If else statement


I was trying a problem and wrote the following code. I have nested if else structures together. Is it correct? Is there any other way I can solve the problem? For example by using for or switch statements?

CPP / C++ / C Code:
/* 
   Write a program which inputs a person’s height (in centimeters) and weight (in
   kilograms) and outputs one of the messages: underweight, normal, or
   overweight, using the criteria:
     Underweight:   weight < height/2.5
     Normal:        height/2.5 <= weight <= height/2.3
     Overweight:    height/2.3 < weight
*/

#include <iostream>

using namespace std;

int main()
{
    float weight, underweight, normal, overweight;
    int height;
    
    cout << "Please enter your height in cm : " << endl;
    cin >> height;
    cout << "Please enter your weight in kg : " << endl;
    cin >> weight;
    
    if (weight<height/2.5)
       cout << "Result : Underweight" << endl;
       else if (height/2.5 <= weight <= height/2.3)
            cout << "Result : Normal" << endl;
            else if (height/2.3 < weight)
                 cout << "Result : Overweight" << endl;
                 
                 
                 return 0;
    }
Last edited by admin : 25-Oct-2009 at 05:00. Reason: Please insert your example C/C++ codes between [CPP] and [/CPP] tags
  #2  
Old 25-Oct-2009, 09:36
fakepoo fakepoo is offline
Regular Member
 
Join Date: Oct 2007
Posts: 713
fakepoo is a jewel in the roughfakepoo is a jewel in the roughfakepoo is a jewel in the rough

Re: If else statement


CPP / C++ / C Code:
else if (height/2.5 <= weight <= height/2.3)
That is not how you want to combine comparisons. Instead, use the && (AND) operator like so:

CPP / C++ / C Code:
else if (height/2.5 <= weight  && weight <= height/2.3)
  #3  
Old 28-Oct-2009, 03:46
saqib_ saqib_ is offline
New Member
 
Join Date: Oct 2009
Posts: 12
saqib_ has a little shameless behaviour in the past

Re: If else statement


Thank you. I will keep that in mind.
 
 

Recent GIDBlogProblems with the Navy (Chiefs) by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
I need help on My SQL Database Creation Statement asdfg MySQL / PHP Forum 6 08-Mar-2008 05:13
PERL If/ else Statement problem Reny Miscellaneous Programming Forum 2 25-Jul-2007 00:20
Is "else if" a separate kind of statement in Java? anders Java Forum 1 20-Apr-2007 16:09
Need a bit of help with switch() Caibre C Programming Language 5 12-Apr-2005 18:02
change statement jilshi Web Design Forum 0 26-Apr-2004 23:40

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 10:15.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.