If Statement

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
27
28
29
#include <iostream>
int main()
{
    using std::cout;
    using std::cin;
    
    int age
    cout << "Enter Your Age: ";
    cin >> age;
    
    if (age > 10)
    cout << "You are young\n";
    
    if (age < 20)
    cout << "Welcome adult\n";
    
    if (age < 30)
    cout << "Wow 30's\n";
    
    if (age < 40)
    cout << "Good job on keeping with the times";
    
    if (age < 50)
    cout << "How is over the hill";
    
    return 0;
}
     


Error I get is
8 `age' does not name a type
9 `age' undeclared (first use this function)

It is simple program to see if I could get the if statement to work. Seems I didn't :P any help would be nice. Thanks
You using statements should be outside of the main function.
Your missing semi-colon at the end of int age

You also want to use

using std::endl;

for

cout << "you are young" << endl;

And use else-statements.

1
2
3
4
if (age <10)
 cout << "young" << endl;
else if (age < 20)
 cout << "not as young" << endl;
Last edited on
Thanks for the fast replay
Now it seems to close soon as I enter the age, is there a way to stop this.
Last edited on
Before the return 0; put system("PAUSE");
Last edited on
Again thanks for fast response and it is now working get.
no worries. Well asked questions typically get good fast responses :)
Also, I think your conditional statements are backwards.

You should flip the signs of your greater-than + less-than signs. ( '<', '>' )
Topic archived. No new replies allowed.