By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,260 Members | 1,338 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,260 IT Pros & Developers. It's quick & easy.

Boolean Operator question

P: n/a
Hi,

I'm trying to understand if statements. I've written the following
code:

#include <iostream>

using namespace std;

int main()
{

int age;

cout<<"Please tell me your age: \n\n";
cin>> age;
cin.ignore();
if (age < 50)
{
cout<<"You're a regular spring chicken, congratulations!\n";
}
else if ( age >= 50 && age <=70)
{
cout<<"You're still looking great, lay off the beer though!\n";
}
else if ( age >= 70 && age <= 100)
{
cout<<"You're entering the twilight of your years, make sure that your
will is in order!\n";
}
else if ( age >= 100)
{
cout<<"Daaamn Methuselah! Whether you're lucky or diligent
congratulations! Here's to clean living...\n";
}

cin.get();
}
It compiles and runs fine, but any age entered over 70 resolves to the
"You're still looking great, lay off the beer though!\n"; statement. In
my understanding the following "else if" statements should report the
other responses. Clearly I'm mistaken though. Could someone offer some
clarity here? Thanks.

promet

Jul 1 '06 #1
Share this Question
Share on Google+
2 Replies


P: n/a
promet wrote:
Hi,

I'm trying to understand if statements. I've written the following
code:

#include <iostream>

using namespace std;

int main()
{

int age;

cout<<"Please tell me your age: \n\n";
cin>> age;
cin.ignore();
if (age < 50)
{
cout<<"You're a regular spring chicken, congratulations!\n";
}
else if ( age >= 50 && age <=70)
{
cout<<"You're still looking great, lay off the beer though!\n";
}
else if ( age >= 70 && age <= 100)
{
cout<<"You're entering the twilight of your years, make sure that your
will is in order!\n";
}
else if ( age >= 100)
{
cout<<"Daaamn Methuselah! Whether you're lucky or diligent
congratulations! Here's to clean living...\n";
}

cin.get();
}
It compiles and runs fine, but any age entered over 70 resolves to the
"You're still looking great, lay off the beer though!\n"; statement. In
my understanding the following "else if" statements should report the
other responses. Clearly I'm mistaken though. Could someone offer some
clarity here? Thanks.

Does it? It shouldn't.

You do have too many <= or >= though one limit should be a < or >, which
response do you expect for 70?

--
Ian Collins.
Jul 1 '06 #2

P: n/a
"promet" <pr*******@sbcglobal.netwrote:
(question about if/else if/ else)
For one thing, your intervals overlap. For another, you fail to
quite understand what "else if" means. Here, I'll take care of
the overlaps and reformat to show just what those else-ifs are
doing:

#include <iostream>
using std::cin;
using std::cout;
using std::endl;
int main()
{
int age;
std::cout<<"Please tell me your age: \n\n";
cin >age;
cin.ignore();
if (age < 50)
{
cout << "... spring chicken ..." << endl;
}
else // age is 50 or more
{
if ( age >= 50 && age <70)
{
cout << "... lay off the beer ..." << endl;
}
else // age is 70 or more
{
if ( age >= 70 && age < 100)
{
cout << "... twilight ..." << endl;
}
else // age is 100 or more
{
cout << "... Daaamn Methuselah! ..." << endl;
}
}
}
return 0;
}

That's what "else if" is really doing for you. It just elides
the "{" between the "else" and the "if" for more readibility.
But in effect, it's nested rather than flat.

--
Cheers,
Robbie Hatley
Tustin, CA, USA
lonewolfintj at pacbell dot net
(put "[usenet]" in subject to bypass spam filter)
http://home.pacbell.net/earnur/
Jul 2 '06 #3

This discussion thread is closed

Replies have been disabled for this discussion.