C++ Averager Exercise

From: TheFueley (thefueley_at_satx.rr.com)
Date: 06/23/04


Date: 23 Jun 2004 12:32:58 -0700

Okay, I'm trying to make an averager type program. It should stop upon
user entering a non-numeric entry, and tally the average with whatever
is in the array already. Searching the list, i came across
cin.clear();
cin.ignore(INT_MAX, '\n');
that didn't work for me, I thought i could use break; to skip the loop
if an alpha is detected. It's not quite working the way I thought it
would. I got everything I need, except for the part about char input
stopping the loop correctly. Currently the program, upon entering a
char, will stop wherever its at and print the rest of the "Donation#4:
", "Donation#5: ", "Donation#6: ", etc. Then give a wrong average. I
know where the prob is, just not how to fix it. Here's what I've got.
/*Write a program that reads up to ten donation values into an array
of double. The program should terminate
* input on non-numeric input. It should report the average of the
numbers and also report how many numbers in the
* array are larger than the average.
*/
#include <iostream>
#include <cctype>
using namespace std;
const int Amount = 10;
double donations[Amount];
double average;
double total_donated = 0.0;
int greater_than_average = 0;
int main()
{
        //get input
        cout << "Enter up to " << Amount << " donations:\n";
        for (int i=0; i<Amount; i++)
        {
                cout << "Donation #" << (i + 1) << ": ";
                cin >> donations[i];
                //stop if input is alpha
                if (isdigit(donations[i]))
                {
                        cin.clear();
                        cin.ignore(INT_MAX, '\n');
                        break;
                }
        }
        //add up donations
        for (int j=0; j<Amount; j++)
        {
                total_donated += donations[j];
        }
        //calculate average
        average = total_donated / Amount;
        cout << "The average donation was: " << average << "\n";
        //find number of donations > average
        for (int k=0; k<Amount; k++)
        {
                if (donations[k] > average)
                        greater_than_average += 1;
        }
        //display # of donations > average
        cout << greater_than_average << " donations were higher than the
average.\n";
        return 0;
}



Relevant Pages

  • Re: C++ Averager Exercise
    ... > user entering a non-numeric entry, and tally the average with whatever ... > * array are larger than the average. ... > int main ... double average(const double *data, size_t entries) ...
    (alt.comp.lang.learn.c-cpp)
  • Re: C++ Averager Exercise
    ... | user entering a non-numeric entry, and tally the average with whatever ... | is in the array already. ... Please Enter another value (other to quit): ...
    (alt.comp.lang.learn.c-cpp)
  • Re: C++ Averager Exercise
    ... > | user entering a non-numeric entry, and tally the average with whatever ... > | stopping the loop correctly. ... > int AboveAverage() const { ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Heap
    ... which shows the creation of heap from an array of elements? ... The user instructions are a little sparse. ... What is the user entering? ...
    (comp.programming)