Operation of with the int age inside loop

It is a while loop inside of whom I want to do the average of the "int age" of the 5 "profile". I don't know how to calculate it. Thanks again!


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
#include <iostream>
#include <string>
#include <cmath>
using namespace std;

string name;
string sexgender;
int age;
int profile;
int x=1;

int main()
{

    do{

    cout << "Profile " << x << "\n\n    Enter name: ";
    cin >> name;
    cout << "    Enter age: ";
    cin >> age;
    cout << "    Finally, enter sex gender (m/f): ";
    cin >> sexgender;
    cout << endl;
        x++;

    }while(x<=5);

    cout << endl << "Number of profiles: " << x-1 << endl;
    cout << endl << "Average age: " << ?????????? << endl;
}
Okay so, create a variable that will hold the average ages and set it to 0.

int avgAge = 0;

in the loop, each time someone enters an age, do this

avgAge = avgAge + age; // a shorter syntax is avgAge += age;

then you can do

cout << endl << "Average age: " << avgAge << endl;
Topic archived. No new replies allowed.