C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a C++ Expert!

Join 300,477 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,751 people online right now. Registration is fast and FREE... Join Now!




Keeping Track of numbers in while statement

 

Keeping Track of numbers in while statement, program doesnt seem to be keeping track of the numbers that are input

jinjelsnaps

17 Sep, 2007 - 07:46 PM
Post #1

New D.I.C Head
*

Joined: 17 Sep, 2007
Posts: 1


My Contributions
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?

CODE

// 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");



}


User is offlineProfile CardPM
+Quote Post


Xing

RE: Keeping Track Of Numbers In While Statement

17 Sep, 2007 - 09:33 PM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 15 times
Dream Kudos: 1575
My Contributions
You would be doing something like this
CODE

// 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");



}

User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 11/8/09 03:33AM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month