C++ | Average Age

/ 25 Sept 2013 /
This is just a simple c++ program using a while loop, not dependant on the code's number of loops, but the user's amount of input, then it just gives the average of the "ages" entered. (Can be any number really.. )




#include <iostream>
using namespace std;

int main() 
{
 int allages = 0, age = 0, numofppl = 0, avage = 0;
 int x = 0;
 while(age != -1) {
  cout << "Please enter age, or 0 to exit: "; cin >> age;
  allages = allages + age;
  numofppl++; 
 }
 allages --;
 numofppl--;
 avage = allages / numofppl;
 cout << "The number of ages entered is: " << numofppl << endl;
 cout << "The average age of all the people: " << avage << endl;
 return 0;
}
 
Copyright © 2010 M(ath)+me, All rights reserved