Need guidance on how to calculate average of all inputs with loops

//Finding smallest number in a series of user input of numbers
#include <iostream>
using namespace std;

int main()
{

int age, smallest = 1000, greatest = 0, age0_18 = 0, age19_30 = 0, age31_40 = 0, age41_60 = 0, over60 = 0;
double sum = 0;

cout << "Enter age of attendee (-1 to quit): ";
cin>> age; //priming need

smallest = greatest = age;

while(age != -1)
{
if(age < smallest)
{
smallest = age;
}

if(age > greatest)
{
greatest = age;
}
if(age >= 0 && age <= 18)
{
age0_18++;
}

else if(age >= 19 && age <= 30)
{
age19_30++;
}

if(age >= 31 && age <= 40)
{
age31_40++;
}

else if(age >= 41 && age <= 60)
{
age41_60++;
}
if( age >= 60)
{
over60++;
}
//prompt for next age input by user
cout << "Enter age of attendee (-1 to quit): ";
cin>> age;
sum+=age;
}
cout << "age 0 to 18: " << age0_18 << endl;
cout << "Age 19 to 30: " << age19_30 << endl;
cout << "Age 31 to 40: " << age31_40 << endl;
cout << "Age 41 to 60: " << age41_60 << endl;
cout << "Over 60: " << over60 << "\n" << endl;
cout << "The average was " << sum/age << endl;
cout << "The youngest person in attendance was " << smallest << endl;
cout << "The oldest person in attendance was " << greatest << endl;

return 0;
}
Why is smallest the bigger number and greatest the smallest number?

Anyway, you're looping it so getting the average should be easy. You want two variables outside the loop. Every time you enter the loop, simple "variable += age;" as you've done with "sum".

But, unlike what you've done, you need one more variable that you should probably call "count" or something similar. Every time the loop runs (at the beginning in your case), it should add 1 to itself - "count++;"

Now, you have the two numbers you need to find average.
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
#include <iostream>

int main()
{

   int count { };
   double sum { };

   int smallest { 50000 };
   int greatest { };

   int age0_18 { };
   int age19_30 { };
   int age31_40 { };
   int age41_60 { };
   int over60 { };

   while (true)
   {
      int age { };

      std::cout << "Enter age of attendee (-1 to quit): ";
      std::cin >> age;

      if (age == -1)
      {
         std::cout << '\n';
         break;
      }
      if (age < smallest)
      {
         smallest = age;
      }

      if (age > greatest)
      {
         greatest = age;
      }
      if (age >= 0 && age <= 18)
      {
         age0_18++;
      }

      else if (age >= 19 && age <= 30)
      {
         age19_30++;
      }

      if (age >= 31 && age <= 40)
      {
         age31_40++;
      }

      else if (age >= 41 && age <= 60)
      {
         age41_60++;
      }
      if (age >= 60)
      {
         over60++;
      }

      sum += age;
      count++;
   }

   std::cout << "age 0 to 18: " << age0_18 << '\n';
   std::cout << "Age 19 to 30: " << age19_30 << '\n';
   std::cout << "Age 31 to 40: " << age31_40 << '\n';
   std::cout << "Age 41 to 60: " << age41_60 << '\n';
   std::cout << "Over 60: " << over60 << "\n\n";

   std::cout << "The average was " << sum / count << '\n';

   std::cout << "The youngest person in attendance was " << smallest << '\n';
   std::cout << "The oldest person in attendance was " << greatest << '\n';

}

Enter age of attendee (-1 to quit): 4
Enter age of attendee (-1 to quit): 18
Enter age of attendee (-1 to quit): 20
Enter age of attendee (-1 to quit): 23
Enter age of attendee (-1 to quit): 31
Enter age of attendee (-1 to quit): 37
Enter age of attendee (-1 to quit): 42
Enter age of attendee (-1 to quit): 90
Enter age of attendee (-1 to quit): 45
Enter age of attendee (-1 to quit): 88
Enter age of attendee (-1 to quit): -1

age 0 to 18: 2
Age 19 to 30: 2
Age 31 to 40: 2
Age 41 to 60: 2
Over 60: 2

The average was 39.8
The youngest person in attendance was 4
The oldest person in attendance was 90
Topic archived. No new replies allowed.