Help Finding: average age, quitting program, and number of males and females entered

Hi there. I am having trouble finishing this. I think my code also might be a little out of order, due to doing this in various steps. Here's the question:

This program is broken down into phases for your convenience only. Please turn in only the final phase. Before turning in your program, please make sure that it does something reasonable if the user enters a negative number the first time.

Phase I: Write a program for a theater that will keep track of how many people in each of 5 age categories attended a particular movie. Use the 5 age categories listed below in the sample screen output. The user will enter a number of ages, entering a negative number when there are no more ages to enter. Your program will then report on how many people in each age group attended. Sample screen output:

Phase II: Modify your program so that, in addition to the report that the program currently produces, it also gives the average age of the people in attendance, the age of the oldest person in attendance, and the age of the youngest person in attendance. Sample screen output:

Phase III: Modify your program so that it also asks for the gender of each attendee. Your program should then break the attendance down by gender as well as by age. Sample screen output:

THIS IS WHAT THE OUTPUT SHOULD BE IN THE END:

Enter age of attendee (-1 to quit): 34
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 16
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 68
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 53
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 39
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 23
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 21
Enter gender (M or F): F
Enter age of attendee (-1 to quit): -1

age 0 to 18: 1
age 19 to 30: 2
age 31 to 40: 2
age 41 to 60: 1
over 60: 1

males: 2
females: 5
The average age was 36.
The youngest person in attendance was 16.
The oldest person in attendance was 68.

**I still need to get the program to quit when -1 is entered, and find the number of males, females, and calculate the average age.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
 #include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

//Variables
int age = 0;
int ageGroupOne, ageGroupTwo, ageGroupThree, ageGroupFour, ageGroupFive;
int youngest;
int oldest;

int counter = 0;

char gender;
char M;
char F;

int main()
{
    for (int i=0; i <=80; i++)
    {
        //Get the age of attendees
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;
        
        //Get the gender of attendees
        cout <<" Enter gender (M or F): ";
        cin >> gender;
        
        if (age < 0)
        {
            cout << "Invalid age \n" << endl;
        }
        
        //Group attendee inputs by age
        else if (age >=0 && age <= 18)
            ++ageGroupOne;
        
        
        else if (age >= 19 && age <= 30)
            ++ageGroupTwo;
        
        
        else if (age >= 31 && age <= 40)
            ++ageGroupThree;
        
        
        else if(age >=41 && age <=60)
            ++ageGroupFour;
        
        
        else if (age >=61 && age <=100)
            ++ageGroupFive;
        
        //Get attendee gender inputs from user
        if (gender == M)
            cin >> M;
        cout << M;
        
        if (gender == F)
            cin >> F;
    }
    
        //Find the youngest and oldest attendee entered
        while (age != -1)
        {
            if (counter == 0)
            {
                oldest = age;
                youngest = age;
            }
            else
            {
                if (age > oldest && age != -1)
                    oldest = age;
                else if (age < youngest && age != -1)
                    youngest = age;
            }
            counter ++;
        }
    
    //Display age groupings
    cout << "Age 0 to 18: ";
    cout << ageGroupOne;
    cout << "\n";
    
    cout << "Age group 19 to 30: ";
    cout << ageGroupTwo;
    cout << "\n";
    
    cout << "Age group 31 to 40: ";
    cout << ageGroupThree;
    cout << "\n";
    
    cout << "Age group 41 to 60: ";
    cout << ageGroupFour;
    cout << "\n";
    
    cout << "Over 60: ";
    cout << ageGroupFive;
    
}


Thanks for your help
Hi,
So what is your current program output now?

Note: You don't need to display information from all 80 people. Just 5 are more than sufficient.
Last edited on
Hi there, thanks for replying. Here's my output:

Enter age of attendee (-1 to quit): 5
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 20
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 38
Enter gender (M or F): M
Enter age of attendee (-1 to quit): 2
Enter gender (M or F): F
Enter age of attendee (-1 to quit): 57
Enter gender (M or F): M
Enter age of attendee (-1 to quit): -1
Enter gender (M or F):
Firstly, you make the input part seperate itself from the calculation part. Use a counter variable to count the number of people you have entered. You should also care for handling possible input errors so that they will not cause possible troubles to your program.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int count = 0;

// Prefer that you use something more efficient like std::vector
int data_age[200];
char data_gender[200];
while(1)
 {
     cout << "Enter age of attendee (-1 to quit): ";
     cin >> age;
     if(age == -1)
     {
        cout << endl;  
        cout << "Input stopped. " << count << " attendees entered\n\n";
        break;
     }   
     cout <<" Enter gender (M or F): ";
     cin >> gender;
        
     if (!cin)
     {
          cout << "Invalid value. Try again. \n\n";
          cin.clear();
          cin.ignore(1000, '\n'); continue;
     }

     if(age < 0 || age > 150)
     {
            cout << "Invalid age. Try again. \n\n";
            continue;
     }
     data_age[count] = age;
     data_gender[count] = gender;
     count++; continue;
 }


You can enter -1 now if you wish to stop inputting

Now update your code and let us know your new program output.
Last edited on
Does that help? :)
Thank you, that helped me figure out how to get the -1 too work. Now I just need help to start calculating the average based on what is entered, and the number of males and females. How do you find the average if you don't know how many inputs will be entered?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <iostream>
#include <iomanip>
#include <cmath>

using namespace std;

int ageGroupOne, ageGroupTwo, ageGroupThree, ageGroupFour, ageGroupFive;
int youngest;
int oldest;

int counter = 0;
int age;

char gender;
char M;
char F;

int main()
{
    int count = 0;
    
    //Age of attendees
    while(1)
    {
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;
        if(age == -1)
        {
            cout << endl;
            cout << "Input stopped. " << count << " attendees entered\n\n";
            break;
        }
        //Gender of attendees
        cout <<" Enter gender (M or F): ";
        cin >> gender;
        
        if (!cin)
        {
            cout << "Invalid value. Try again. \n\n";
            cin.clear();
            cin.ignore(1000, '\n'); continue;
        }
        
        if (age < 0)
        {
            cout << "Invalid age \n" << endl;
        }
        
        //Age groups of attendees
        else if (age >=0 && age <= 18)
            ++ageGroupOne;
        
        
        else if (age >= 19 && age <= 30)
            ++ageGroupTwo;
        
        
        else if (age >= 31 && age <= 40)
            ++ageGroupThree;
        
        
        else if(age >=41 && age <=60)
            ++ageGroupFour;
        
        
        else if (age >=61 && age <=100)
            ++ageGroupFive;
    }
        //Oldest and youngest attendees
        while (age != -1)
        {
            if (counter == 0)
            {
                oldest = age;
                youngest = age;
            }
            else
            {
                if (age > oldest && age != -1)
                    oldest = age;
                else if (age < youngest && age != -1)
                    youngest = age;
            }
            counter ++;
        }
    
   //Age groups of attendees displayed
    cout << "Age 0 to 18: ";
    cout << ageGroupOne;
    cout << "\n";
    
    cout << "Age group 19 to 30: ";
    cout << ageGroupTwo;
    cout << "\n";
    
    cout << "Age group 31 to 40: ";
    cout << ageGroupThree;
    cout << "\n";
    
    cout << "Age group 41 to 60: ";
    cout << ageGroupFour;
    cout << "\n";
    
    cout << "Over 60: ";
    cout << ageGroupFive;
    
}



OUTPUT:
Enter age of attendee (-1 to quit): 5
Enter gender (M or F): m
Enter age of attendee (-1 to quit): 27
Enter gender (M or F): f
Enter age of attendee (-1 to quit): 38
Enter gender (M or F): m
Enter age of attendee (-1 to quit): 65
Enter gender (M or F): f
Enter age of attendee (-1 to quit): 24
Enter gender (M or F): m
Enter age of attendee (-1 to quit): -1

Input stopped. 0 attendees entered

Age 0 to 18: 1
Age group 19 to 30: 2
Age group 31 to 40: 1
Age group 41 to 60: 0
Over 60: 1
Have you learned array?
> How do you find the average if you don't know how many inputs will be entered?
I did include a counter variable in my example. It is just that you ignored it and deleted it.
The problem is I haven't really learned anything, which is why I am struggling so much. It's an online accelerated pace summer class.....and all I have to learn from is the textbook. So, I remember reading about arrays, but still haven't read everything about them. Still trying to figure most of this out, and it's pretty challenging to teach this stuff to yourself when you've never done it before. Nevertheless, I'm trying, and am super appreciate of all the help I've received on this forum. So, if you have a minute and wouldn't mind giving me a quick explanation, I'd really appreciate it. Thanks so much

For the counter variable, I only ignored/deleted what was giving me an error. Maybe I am putting it in the wrong place?

Edited to say: Looking back in my book now, it looks like we haven't covered arrays yet, it was just briefly mentioned. It's still 2 chapters ahead
Last edited on
Then you go without array help then...

Note : If you haven't learned array just forget it. But life will be much easier if you use array.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
count = 0;
int ageTotal = 0; // needed to calculate the average age 
int nMales = 0;
int nFemales = 0;
while(1)
 {
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;
        if(age == -1)
        {
            cout << endl;
            cout << "Input stopped. " << count << " attendees entered\n\n";
            break;
        }
        // Gender of the attendee
        cout <<" Enter gender (M or F): ";
        cin >> gender;
        
        if (!cin)
        {
            cout << "Invalid value. Try again. \n\n";
            cin.clear();
            cin.ignore(1000, '\n'); continue;
        }
        
        if (age <= 0 || age > 150)
        {
            cout << "Invalid age \n" << endl;
        }

        // This part!!!
        // After you validate the input, you accept the person and increase the counter by 1
       count = count + 1;

        // To get the average number, you calculate the total (just sum them up)
       ageTotal = ageTotal + age;

        // You compare to check if the attendee is the youngest or the oldest
        if(age < youngest) youngest = age;
        if(age > oldest) oldest = ???;

        // You count the number of male & female attendees
        if(gender == 'M') nMales = nMales + 1;
        if(gender == ???) nFemales = ??? + 1;

        if (age >= 0 && age <= 18) ++ageGroupOne;
        else if (age >= 19 && age <= 30) ++ageGroupTwo;
        else if (age >= 31 && age <= 40) ++ageGroupThree;                else if(age >= 41 && age <= 60) ++ageGroupFour;
       else if (age >= 61 && age <= 150) ++ageGroupFive;
  }
Last edited on
Does that? :)
Like closed account said using array will help a lot and also shorten your code.... you have to learn it someday, why not now?
Thanks, that does help. I was still getting errors so I tried to edit it more. Only problem now is nothing is calculating correctly. Definitely wasn't cut out for coding, I've been at this for 5 hours now when it would probably normally only take someone 30 minutes :)

Here's my newest problematic code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Example program
#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>

using namespace std;

int main()
{
    int count = 0;

    int ageTotal = 0; 
    int ageGroupOne, ageGroupTwo, ageGroupThree, ageGroupFour, ageGroupFive;
    int age;
    int youngest;
    int oldest;

    int nMales = 0;
    int nFemales = 0;
    char gender;
    
    while(1)
    {
        //Attendee age inputs
        cout << "Enter age of attendee (-1 to quit): ";
        cin >> age;
        if(age == -1)
        {
            cout << endl;
            cout << "Input stopped. " << count << " attendees entered \n";
            break;
        }
        // Attendee gender inputs
        cout <<" Enter gender (M or F): ";
        cin >> gender;
        
        // Error message
        if (!cin)
        {
            cout << "Invalid value. Try again. \n\n";
            cin.clear();
            cin.ignore(1000, '\n'); continue;
        }
        
        // Error message
        if (age <= 0 || age > 150)
        {
            cout << "Invalid age \n" << endl;
        }

        // Counter
        count = count + 1;
       
        if (age >=0 && age <= 18)
            ++ageGroupOne;
        
        if (age >= 19 && age <= 30)
            ++ageGroupTwo;
        
        if (age >= 31 && age <= 40)
            ++ageGroupThree;
        
        if(age >=41 && age <=60)
            ++ageGroupFour;
        
        if (age >=61 && age <=100)
            ++ageGroupFive;
        
    }

        // Average Attendee Age
        ageTotal = ageTotal + age;
        cout << "The average age was: " << ageTotal << endl;

        // Youngest and Oldest Attendees
        if(age < youngest) youngest = age;
        cout << "The youngest person in attendance was: " << youngest << endl;
        if(age > oldest) oldest = age;
        cout << "The oldest person in attendance was: " << oldest << endl;

        // Number of Male and Female Attendees
        if(gender == 'M') nMales = nMales + 1;
        cout << "Males in attendance: " << nMales << endl;
        if(gender == 'F') nFemales = nFemales + 1;
        cout << "Females in attendance: " << nFemales << endl;
        
        //Age groups of attendees displayed
        cout << "Age 0 to 18: ";
        cout << ageGroupOne;
        cout << "\n";
    
        cout << "Age group 19 to 30: ";
        cout << ageGroupTwo;
        cout << "\n";
    
        cout << "Age group 31 to 40: ";
        cout << ageGroupThree;
        cout << "\n";
    
        cout << "Age group 41 to 60: ";
        cout << ageGroupFour;
        cout << "\n";
    
        cout << "Over 60: ";
        cout << ageGroupFive;
        
}


OUTPUT:
Enter age of attendee (-1 to quit): 14
Enter gender (M or F): m
Enter age of attendee (-1 to quit): 19
Enter gender (M or F): f
Enter age of attendee (-1 to quit): 34
Enter gender (M or F): m
Enter age of attendee (-1 to quit): -1

Input stopped. 3 attendees entered
The average age was: -1
The youngest person in attendance was: -1
The oldest person in attendance was: 0
Males in attendance: 0
Females in attendance: 0
Age 0 to 18: 1
Age group 19 to 30: 1
Age group 31 to 40: -569352575
Age group 41 to 60: 4198182
Over 60: 0
Thirdly,
1
2
3
4
int ageGroupOne, ageGroupTwo, ageGroupThree, ageGroupFour, ageGroupFive;
   
    int youngest;
    int oldest;


Always initialise your necessary variables. They will simply start with random (garbage) values if left uninitialized.
1
2
3
4
5
int ageGroupOne = 0, 
ageGroupTwo = 0, ageGroupThree = 0, ageGroupFour = 0, ageGroupFive = 0;
    
    int youngest = 0;
    int oldest = 0;
Also :
1
2
3
4
5
// Number of Male and Female Attendees
...

// Youngest and Oldest Attendees
...


Please put these things in the while loop.
1
2
3
4
if (age <= 0 || age > 150)
{
            cout << "Invalid age \n" << endl;
}

You need to add a "continue" command so that the person with the invalid age will not continue to be considered.

==>
1
2
3
4
if (age <= 0 || age > 150)
{
            cout << "Invalid age \n" << endl; continue;
}
Last edited on
Does that help? : )
Topic archived. No new replies allowed.