1. ## Noob question

I know this is a very simple problem but I cant figure it out. I have been doing the tutorials and trying to learn but I got stuck on the IF... I know, kind of sad.
This is my code:

Code:
#include <iostream>

using namespace std;

int main()
{
int age;

cout<<"How old are you? ";
cin>> age;
cin.ignore();
if ( age < 50 ) {
cout<<"You are under 50! I know, I'm amazing!\n";
}
else if ( age == 14 || 15) {
cout<<"Blah/n";
}
else if ( age == 100 ) {
cout<<"You're older than my grandmother!\n";
}
else {
cout<<"You're really old\n";
}
cin.get();
}
Everything works except if age equals 15 or 14. If age equals 15 or 14 it still executes if ( age < 50 ) {....
Can you tell me whats wrong. Please.

-Thx

2. When you are entering 14 or 15 it is still less than 50 so it executes that.

Now find a way to fix that. =)

3. Code:
else if ( age == 14 || 15) {

//should be...
else if ( age == 14 || age == 15) {
A better way to write it.
Code:
if( age < 15 ) {
cout<<"Blah/n";
}
else if( age < 50 ) {
cout<<"You are under 50! I know, I'm amazing!\n";
}
else if ( age == 100 ) {
cout<<"You're older than my grandmother!\n";
}
else {
cout<<"You're really old\n";
}

4. Originally Posted by sand_man
Code:
else if ( age == 14 || 15) {

//should be...
else if ( age == 14 || age == 15) {
A better way to write it.
Code:
if( age < 15 ) {
cout<<"Blah/n";
}
Thank you for the help. That worked. But for what I was doing < 15 was to broad of a range.

5. To fix that, put your else-ifs in numerical order..

(This is pseudocode cause im too lazy to type it in real code)

if age < 40
if age < 20
if age < 30

do:

if age <20
if age<30
if age <40

With the first way, if age is 2 it will execute the code for if the age is less than 40 and then skip the others. With the second, if it's 2, it will execute the 20 and then skip the others (desired result)