Add two variables to keep track of the count of numbers, and the total from adding up all the numbers (apologies for the English names).
Then at the end of the loop you can divide the total by the count and that is your average.
int total = 0;
int count = 0;
...
while(...)
{
...
string str = a;
total += atoi(str.c_str());
++count;
}
cout << "average is " << (double)total/count << '
';
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…