Average Calculating Wrong in the Loop

Hello. I'm wondering if someone can help me fix my code. I have everything set-up, except my output numbers are not calculating correctly. My counts for the number of males and females are off. They are not counting separately. Also, I'm not sure why my youngest person is showing up as 0, when it should be 16. Thank you

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

#include <iostream>
#include <iomanip>
#include <cmath>

//Variables
using namespace std;

int main()
{
    int age, ageTotal = 0, ageGroupOne = 0, ageGroupTwo = 0, ageGroupThree = 0, ageGroupFour = 0, ageGroupFive = 0;
    int sum=0, counter = 0, youngest = 0, oldest = 0;
    int nMales=0, nFemales=0;
    char gender, M, F;
    
    youngest = age;
    oldest = age;
    
    cout << "Enter age of attendee (-1 to quit): ";
    cin >> age;
    
    cout <<"Enter gender (M or F): ";
    cin >> gender;
    
    //Loop to get input of ages and genders of attendees
    while(age != -1)
    {
        //Get age input
        if (age >= 0)
        {
            counter++;
            sum = sum + age;
            cout << "Enter age of attendee (-1 to quit): ";
            cin >> age;
            
            //Terminate before asking for gender if -1 is entered.
            if (age == -1)
            break;
            
            //Get gender input
            cout <<"Enter gender (M or F): ";
            cin >> gender;
            nMales++;
            nFemales++;
        }
        
        //Group ages by age range starting with control variable.
        if (age < 0)
        {
            cout << "Invalid age \n" << endl; continue;
        }
        
        //Counts 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;
            
        //Counts oldest and youngest attendees
        if (age < youngest)
        youngest = age;
        
        if (age > oldest)
        oldest = age;
    }
    
    //Outside the loop. Display outputs and calculations.
    
    //AGE GROUPS OF ATTENDEES
    cout << "Ages 0 to 18: " << ageGroupOne << endl;
    
    cout << "Ages 19 to 30: " << ageGroupTwo << endl;
    
    cout << "Ages 31 to 40: " << ageGroupThree << endl;
    
    cout << "Ages 41 to 60: " << ageGroupFour << endl;
    
    cout << "Over 60: " << ageGroupFive << endl;
    
    //NUMBER OF MALES
    cout << "Males: " << nMales << endl;

    //NUMBER OF FEMALES
    cout << "Females: " << nFemales << endl;
    
    //AVERAGE AGE
    cout << "The average age was "<< sum/counter << endl;
    
    //YOUNGEST PERSON
    cout << "The youngest person in attendance was " << youngest << "." << endl;
    
    //OLDEST PERSON 
    cout << "The oldest person in attendance was " << oldest << "." << endl;
    
    system ("PAUSE");
    return 0;
}
Last edited on
I actually got the average working now, but am still having trouble with my other issues.
Topic archived. No new replies allowed.