-
New to C++
hello, im new to C++ and I have a question about a problem I am having. I am useing the If statements to ask questions and the question is
How old are you?
I got the coding right and it all works but I am having a problem.
I have
Code:
if ( Age < 50 ) {
cout<<"Well your not over the hill yet haha.\n";
}
else if ( Age == 50 ) {
cout<<"Hmm, congratulations on making it this far without being shot.\n";
}
else if ( Age > 50 ) {
cout<<"well your over the hill and falling.....\n";
}
if ( Age >= 100 ) {
cout<<"OH MY GOD!\n";
}
cin.get();
It all works..... Except the OH MY GOD part of it.... I want to put in the coding "Equal to or Greater than 100"
so what I have here is if you are under 50 you get the first responce. If your equal to 50 you get the second... and if you are above 50 you get the 3rd...
If you are over OR equal to 100 or over I want it to say the 4th responce. how do i fix this?
-
How about something like this:
Code:
if ( Age < 50 ) {
cout<<"Well your not over the hill yet haha.\n";
}
else if ( Age == 50 ) {
cout<<"Hmm, congratulations on making it this far without being shot.\n";
}
else if ( Age > 50 && age < 100 ) {
cout<<"well your over the hill and falling.....\n";
}
if ( Age >= 100 ) {
cout<<"OH MY GOD!\n";
}
cin.get();
This logic will ensure that 'age' will only qualify for a single case.
suggestion:
Not sure if you have looked into switch/case structure yet, but swtich/case is a good way to handle a bunch if if/else statements.
-
Better still:
Code:
if (age < 50 ) {
cout<<"Well you're not over the hill yet haha.\n";
}
else if ( age == 50 ) {
cout<<"Hmm, congratulations on making it this far without being shot.\n";
}
else if (age < 100 ) {
// No need to test if age > 50 as it must be if this gets executed.
cout<<"Well you're over the hill and falling.....\n";
}
else {
cout<<"OH MY GOD!\n";
}
cin.get();
And I even corrected your spelling ;)