# begginers if statement problem

• 01-31-2005
lastresort
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(); }```
• 01-31-2005
fuh
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.
• 01-31-2005
Strait
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?
• 01-31-2005