# Keeping Track of numbers in while statement

Page 1 of 1

## 1 Replies - 3768 Views - Last Post: 17 September 2007 - 10:33 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=33478&amp;s=940c840db28431549064e2ab666dc284&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 jinjelsnaps

Reputation: 0
• Posts: 1
• Joined: 17-September 07

# Keeping Track of numbers in while statement

Posted 17 September 2007 - 08:46 PM

So my program doesnt seem to be keeping track of the numbers that are inputed for the ages. It takes the last number inputed (which is the input to terminate the loop) and makes that my age variable. How can I make the age variable not be what is inputed last?

```// This program keeps track of how many people in 5 different
// age categories attend a particular movie

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int age;		  // age the user types in
int count;
int sumSoFar;	 // sum of all ages
int youngest;	 // youngest age
int oldest;	   // oldest age
int group1;
int group2;
int group3;
int group4;
int group5;
char n;

sumSoFar = 0;
count = 0;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;
while (age != -7){
sumSoFar = sumSoFar + age;
if (age < youngest){
youngest = age;
}
if (age > oldest){
oldest = age;
}
cout << "enter age of attendee (-1 to quit): ";
cin >> age;
count++;

}

if (count == 0){
cout << "There is no average, because you did not "
<< "enter any numbers!";
} else {

cout << "The average age was "
<< sumSoFar/count << endl;

}
if (youngest == -1){
cout << "There is no youngest because you "
<< "did not enter any numbers!";
} else{
cout << "The youngest person in attendance was "
<< youngest << endl;
}

if (oldest == -1){
cout << "There is no oldest because you "
<< "did not enter any numbers!";
} else{
cout << "The oldest person in attendance was "
<< oldest << endl;
}

count == 0;
if (age < 0);
cout << "no age entered" << endl;
if (age <= 18)
group1 = (age <= 18);
cout << "age 0 to 18: " << age << endl;
if (age <= 30)
group2 = (age <= 30);
cout << "age 19 to 30: " << age << endl;
if (age <= 40)
group3 = (age <= 40);
cout << "age 31 to 40: " << group3 << endl;
if (age <= 50)
group4 = (age <= 60);
cout << "age 41 to 60: " << group4 << endl;
if (age > 60)
group5 = (age > 60);
cout << "over 60: "  << group5 << endl;

system("pause");

}

```

Is This A Good Question/Topic? 0

## Replies To: Keeping Track of numbers in while statement

### #2 Xing

Reputation: 19
• Posts: 725
• Joined: 22-July 06

## Re: Keeping Track of numbers in while statement

Posted 17 September 2007 - 10:33 PM

You would be doing something like this
```// This program keeps track of how many people in 5 different
// age categories attend a particular movie

#include <iostream>
#include <cmath>
using namespace std;

int main()
{
int age;		  // age the user types in
int count;
int sumSoFar;	 // sum of all ages
int youngest;	 // youngest age
int oldest;	   // oldest age
int group1 = 0;	   //0 to 18
int group2 = 0;	   //19 to 30
int group3 = 0;	   //31 to 40
int group4 = 0;	   //41 to 60
int group5 = 0;	   //over 60

sumSoFar = 0;
count = 0;
cout << "Enter age of attendee (-1 to quit): ";
cin >> age;
youngest = oldest = age;
while (age != -1)
{
sumSoFar = sumSoFar + age;
if (age < youngest)
{
youngest = age;
}
if (age > oldest)
{
oldest = age;
}

if (age <= 18)
group1++;
else if (age >18 && age <= 30)
group2++;
else if (age >30 && age <= 40)
group3++;
else if (age >40 && age <= 50)
group4++;
else
group5++;

cout << "enter age of attendee (-1 to quit): ";
cin >> age;
count++;

}

if (count == 0)
{
cout << "There is no average, because you did not "
<< "enter any numbers!";
}
else
{

cout << "The average age was " << sumSoFar/count << endl;

}
if (youngest == -1)
{
cout << "There is no youngest because you " << "did not enter any numbers!";
}
else
{
cout << "The youngest person in attendance was " << youngest << endl;
}

if (oldest == -1)
{
cout << "There is no oldest because you "
<< "did not enter any numbers!";
}
else
{
cout << "The oldest person in attendance was "
<< oldest << endl;
}

cout << "age 0 to 18: " << group1 << endl;
cout << "age 19 to 30: " << group2 << endl;
cout << "age 31 to 40: " << group3 << endl;
cout << "age 41 to 60: " << group4 << endl;
cout << "over 60: "  << group5 << endl;

system("pause");

}

```