# Ages

• 11-30-2011
Pookie
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.

Code:

```#include <iostream> #include <iomanip> #include <cmath> using namespace std; int main() {         int age(0), sex(0), youngest(999), oldest(0), M(0), F(0), age1(0), age2(0), age3(0), age4(0), age5(0), totalage(0), totalcount(0);                 do         {                 cout << "Enter sex \n" <<endl;                 cin >> sex;                                                 if (sex != M || sex != F)                         {                         cout << "Invalid sex entered \n" << endl;                         break;                         }                                         cout << "Enter age of attendee \n" << endl;                 cin >> age;                         if (age < 0)                         {                         cout << "Invalid age \n" << endl;                         break;                         }                                                 if (age >=0 && age <= 18)                                         ++age1;                                                 if (age >= 19 && age <= 30)                                         ++age2;                                                 if (age >= 31 && age <= 40)                                         ++age3;                                                 if(age >=41 && age <=60)                                         ++age4;                                                 if (age >=61 && age <=100)                                         ++age5;                                                 if (age < youngest)                                         youngest += age;                         if (age > oldest)                                         oldest +=age;                 ++ totalcount;                 ++ totalage;                                 } while (age > 0 );                 return 0; }```
This is what I have so far and I am very close to breaking it, but I can't seem to reach that point. Help?
• 11-30-2011
bernt
Code:

```int sex(0), M(0), F(0); ... cout << "Enter sex \n" <<endl; cin >> sex;             if (sex != M || sex != F) {     cout << "Invalid sex entered \n" << endl;     break; }```
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.
• 11-30-2011
Pookie
Ages
The purpose of that second statement is to give an error when the user enters another variable, say....A or B. Although I changed what you suggested, I am still getting an error.
• 11-30-2011
bernt
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?
• 11-30-2011
Pookie
Ages
I have been working on this code for SO long now :( And I'm really close to breaking it :/ But I'm stuck.
• 12-01-2011
Elysia
You must learn to differentiate

if (X == A)
and
if (X == 'A')

The first says, if X is equal to the contents in A, and the second one says if X is equal to the character A.