Noob question! (IF and Else)

Hey guys! I got a problem right here...

When I run the program and choose an age under 18 in I'll get ""Hey kid, go to bed!"" which is good :p

But when I test and pick an age over 18, i get both the
"Oh man you're old!"
AND
"Hey kid, go to bed!"

What have I done wrong and what should I do to solve this?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include "stdafx.h"
#include <iostream>
#include <string>

int _tmain(int argc, _TCHAR* argv[])
{

	using namespace std;

	int age;

	cout << "Hey, how old are you?" << endl;

	cin >> age;

		if (age > 18)
			cout << "Oh man you're old!\n";
		else (age < 18); 
			cout << "Hey kid, go to bed!\n"

	cin.get();
	cin.get();


	return 0;
}


(I'm a newbie as you can see)

Thanks!
Last edited on
I can't believe this compiles..

else (age < 18);
cout << "Hey kid, go to bed!\n"
is bad syntax.. you even forgot the ';' after cout

use either

else {
cout << "Hey kid, go to bed!\n" }

or

else if (age < 18) { ..
else (age < 18);

Is as if you wrote it like this, at least this is how the compiler interprets it.

1
2
3
4
5
6
7
8
9
if (age > 18)
{
   cout << "Oh man you're old!";
}
else
{
    age<18;
}
cout << "Hey kid, go to bed!";


You either need to do else if (age < 18) or just plain old else //Without the condition
line 16-19 is equivalent to:
1
2
3
4
5
6
7
8
9
if (age > 18)
{
	cout << "Oh man you're old!\n";
}
else 
{
	age < 18;
}
cout << "Hey kid, go to bed!\n"


just remove the (age < 18); after else and your code should work fine.
@Peter87 Jinx.. 1.. 2.. 3..
clanmjc wrote:
@Peter87 Jinx.. 1.. 2.. 3..

Does that mean I will have bad luck?
ah alright! It's working now ;p

Thanks guys!
Last edited on
Does that mean I will have bad luck?

According to my kids it is equivalent to "the medal of honor" in coolness.
Topic archived. No new replies allowed.