# Thread: [Noob Needing Help] Not too sure I understand!

1. ## [Noob Needing Help] Not too sure I understand!

Hey all, so I'm currently in my last year in school and want to go down the career path of programming. So I decided to begin learning early. I want to learn C++ and have followed lessons 1 and 2 on this site. However when coming to test my skills I'm currently stuck and don't know whether or not I've understood correctly.

Code:
```#include <iostream>

using namespace std;

int main()
{
int age;
int current_year = 2012;

cin>> age; //Recives user input as their age
cin.ignore();

//Repeats age then works out when the person was born by taking the age away from 2012 or 2011 incase their birthday hasn't came around yet
cout<<"Your age is: "<< age <<" which mean you where either born in "<< current_year - age <<" or "<< (current_year - 1) - age<<"\n";

if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
{
cout<<"You are pretty young!";
}

else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
{
cout<<"You are a teen!";
}

else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
{
cout<<"You middle aged";
}

else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
{
cout<<"You are quite old!";
}
}```
This is the current program I am trying to write, where you enter your age in, it tells you what year you may have been born in the says where your young, old, or middle aged.

However I am getting a few problems:

1. When trying to run the program it is saying that:
Code:
``` else if (age > 10 && < 20)
{
...
}

else if (age > 20 && < 50)
{
...
}```
is incorrect.

2. That when I press enter it goes through the whole program, where as I want you to enter you name, press enter, it tells you there year, press enter, it tells you young, old, etc.

Any help that you guys can offer would be a joy.

// Kegs

2. You need to specify what you're comparing against in the second clause of the if statement:
Code:
`else if (age > 10 && age < 20)`

3. Works like a treat, Thanks.

Do you anyway I can solve my second problem?

4. Originally Posted by Kegs
Do you anyway I can solve my second problem?
You probably need to write code to do what you intend.

You want the user to enter a name and press enter. Your code is only reading an age; it is neither prompting for nor reading a name.

Code:
```int name;

cin<< name;
cin.ignore();```
However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
(P.S Sorry for asking so many questions)

6. Code:
```if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
{
cout<<"You are pretty young!";
}

else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
{
cout<<"You are a teen!";
}

else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
{
cout<<"You middle aged";
}

else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
{
cout<<"You are quite old!";
}```
What will happen if you enter 10, 20, or 50?

7. Originally Posted by Matticus
What will happen if you enter 10, 20, or 50?
Didn't think about that, added in '<=' and '>=' where unnecessary. Thanks

8. Originally Posted by Kegs

Code:
```int name;

cin<< name;
cin.ignore();```
However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
(P.S Sorry for asking so many questions)
So first, a name is a string. Therefore, you should be using std::string, not int (why are you using int?).
As an advice, when reading a string, it is better to use std::getline instead of cin >> since it stops reading at first whitespace (space, tab, newline).

Code:
```if (age < 10) //Asks if your less than 10 years old, then tells you that you're pretty young
{
cout<<"You are pretty young!";
}

else if (age > 10 && < 20) //Asks if you are between 10 and 20 and then tells you that you're a teen
{
cout<<"You are a teen!";
}

else if (age > 20 && < 50) //Asks if you are between 20 and 50 and then tells you that you're middle-aged
{
cout<<"You middle aged";
}

else if (age > 50) //Asks that if you are over 50, and then tells you that you're quite old
{
cout<<"You are quite old!";
}```
Now, as for this, you can simplify this to:
Code:
```if (age < 10)
cout<<"You are pretty young!";
else if (age < 20) // Age must be >= 10 here, else first if clause would match.
cout<<"You are a teen!";
else if (age < 50) // Age must be >= 20 here.
cout<<"You middle aged";
else // Anything else basically. Will implicitly mean age >= 50 here.
cout<<"You are quite old!";```
Braces for single line statements are not necessary. Whether it is good practice to do or not I will not comment on.
If you find yourself making lots of bugs with adding another line to an if statement which has no braces and you forget to add braces, you may want to always use them.

9. Originally Posted by Elysia
(why are you using int?).
Sorry like I said, I'm still very new and just read over the first 2 lessons once. I'm not quite sure what a string is that's why I didn't use it, I thought that int would be ok to user here. I'm going to look into strings now and post back when updated.

EDIT: Looked into this and was easier to do than thought. Since I already had 'using namespace std;' I just changed int to string. Now works like I wanted to thanks all for the help.

10. Originally Posted by Kegs

Code:
```int name;

cin<< name;
cin.ignore();```
However when I press enter it just goes through the whole program without pausing for me to enter my age, then it to repeat that.
(P.S Sorry for asking so many questions)

so here you are declaring name as int (or string now that you've changed it) but you are telling the computer to display 'name' before any data is stored to it.. try flipping the order, also try using getline so you can accept a first and last name if it is entered.

Code:
```string name; ////declaring name as a string

cout<<"please enter your name\n";      ///if you ran the program without asking first, how will anyone know what to enter??
getline(cin, name);            ////the computer reads in the entire line, even if a space is entered. If you use cin>>, the computer will stop reading at the first "whitespace".
cout<<"\nyour name is: " << name;     /// output statement and whatever name the user entered.```
hope this helps, and whenever you get stuck just keep at it!! it will all eventually click in your mind.

11. Seems to work a lot better using 'getline', Thanks.