Originally Posted by
Kegs
So I've added in,
Code:
int name;
cout<<"/nYour name is: "<< 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.