Ages

This is a discussion on Ages within the C++ Programming forums, part of the General Programming Boards category; Hey guys
I just started working on a problem that states "Write a C++ program for a theater that will ...

Ages

Hey guys

I just started working on a problem that states "Write a C++ program for a theater that will keep track of a movie attendee. The program should ask the user to enter their eage and gender. The user should enter a negative number when there are no more ages to enter. The program should then break down the attendees by age and gender. The following age categories should be used:

0-18 years
19-30 years
31 - 40 years
41 - 60 years
Over 60 years

The program should also display how many males and females attended the movie, in addition to computing and displaying the age of all attendees, the age of the youngest attendee, and that of the oldest.

When you ask cin to read in an int, it's going to look for digits, and if it doesn't find digits (ie, if you type in 'F' or 'M') cin is going to fail.
On the other hand, if you ask cin to read in a char, things will work more like I think you expect it to: cin will read in one character and put that in your variable.

Second,

Code:

(sex != M || sex != F)

What does this actually do? Keep in mind, you're comparing integers, and variable names shouldn't affect the result of the comparison.

But when you enter 'M' or 'F' in the console, cin does nothing to associate those characters with a variable in your program. And when you compare (sex != M || sex != F), that has nothing to do with whether sex contains 'M' or 'F'.

Let's consider what's in these variables (sex, M, and F) once you get to that point.
Say sex contains the character 'M'. M and F are set to 0, per the values you gave for initializing them. Then this statement becomes

Code:

if ('M' != 0 || 'M' != 0)

Say sex contains 'F' or 'A' or '0' ('0' != 0, by the way, characters are stored according to the ASCII code)... do you see why this is incorrect?