Thread: begginers if statement problem

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    15

    begginers if statement problem

    Here's my problem. I have just finished reading up on the code tutorials and i have programed this little thing. The only problem is that when i enter the age 30 and the # of children as <=0 it says what its supposed to say at age 20 and # of children as <=0. Some goes with age 30 and # of children as 1 and age 20 and # of children as >=1. Also with the age 30 and # of children as 5 and age 20 and # of children as >=1.

    Here is my code.

    Code:
    #include <iostream>    
    
    using namespace std;
            
    int main()                            
    {
      int age;
      int children;                            
      
      cout<<"Please enter your age: ";    
      cin>> age;
      cout<<"Please enter the number of children you have: ";
      cin>> children;                          
      cin.ignore();                       
      if ( (age < 20 && children <= 0) ) {                  
        cout<<"How cute, you're not even married yet!\n"; 
      }
      else if ( age <= 20 && children >= 1 ) {
        cout<<"Thats disgusting... really.\n";
      }
      else if ( age >= 21 && children <= 0 ) {            
        cout<<"Getting ready to get a little action?\n";
      }   
      else if ( age >= 21 && children >= 1 ) {
        cout<<"Not bad.  I hope you can still find the time to go to college\n";     
      }
      else if ( age >= 30 && children <= 0 ) {
        cout<<"Lonely little feller now aren't we. :(\n";
      }
      else if ( age >= 30 && children >= 1 ) {
        cout<<"You're perfect.\n";
      }
      else if ( age >= 30 && children >= 5 ) {
        cout<<"Busy little feller aren't we!\n";
      }
      cin.get();
    }

  2. #2
    Attack hamster fuh's Avatar
    Join Date
    Dec 2002
    Posts
    176
    Code:
      else if ( age >= 30 && children >= 1 ) {
        cout<<"You're perfect.\n";
      }
      else if ( age >= 30 && children >= 5 ) {
        cout<<"Busy little feller aren't we!\n";
      }
      cin.get();
    }
    It has something to do with the way you ordered the statements. Right here in my quote if the person inputs six children and an age greater than thirty the "You're perfect" message will fire and since it's an else if the "Busy little feller" message doesn't have a chance to fire. Also, in a nitpicking way, you should change <=0 to ==0 and have an error message if they enter negative numbers.
    Stupid things pop singers say

    "I get to go to lots of overseas places, like Canada."
    - Britney Spears

    "I love what you've done with the place!"
    -Jessica Simpson upon meeting the Secretary of Interior during tour of the White House

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    46
    you need to add another condition to some of those statements

    Code:
    else if ((age >= 21) && (age < 30) && (children <= 0 )) {            
        cout<<"Getting ready to get a little action?\n";
      }   
      else if ((age >= 21) && (age < 30) && (children >= 1 )) {
        cout<<"Not bad.  I hope you can still find the time to go to college\n";
    see what I'm sayin?

  4. #4
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    The problem is that if age is 30 and children is <= 0 then the statement
    >> else if ( age >= 21 && children <= 0 )
    will be true and it won't even have a chance to display what you want it to
    you could revise it to
    else if (age >= 21 && age < 30 && children <= 0)
    Also, if you enter the age as 20 and children as <= 0 then nothing will happen because of
    >>if ( (age < 20 && children <= 0) )

    EDIT
    Arg, beat again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch statement problem
    By jalex39 in forum C Programming
    Replies: 6
    Last Post: 03-08-2008, 04:05 PM
  2. having problem with string statement during a loop!
    By Hp7130p in forum C++ Programming
    Replies: 5
    Last Post: 04-21-2005, 09:40 AM
  3. If statement re-do problem
    By RoD in forum Windows Programming
    Replies: 5
    Last Post: 09-11-2002, 04:46 PM
  4. Major Problem
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 02-19-2002, 01:06 PM