Hi,
I trying to let the user input how many numbers that they want to average and outputting the average of those numbers. I have this code, but not working as aspected.

#include <iostream>
using namespace std;

int main ()
{
int numCount, total;
double average;
cout << "How many numbers do you want to average? ";
for (int count = 0; count < numCount; count++)
{
total = 0;
int num;
cout << "Enter a number: ";
cin >> num;
count++;
}
average = total / numCount;
cout << "The average is " <<average<<endl;
return 0;
}

Dani AI

Generated

— the fixes in ’s reply point to the root problems (using numCount before reading it, leaving variables uninitialized, and not adding each input to the running total). Below are a few practical rules and a robust pattern you can use to avoid those pitfalls and produce correct averages every time.

Key points to follow

  • Read and validate the count before using it; refuse zero or negative counts.
  • Initialize your accumulator (use double for the running sum if you expect fractional results).
  • Don’t accidentally increment the loop counter twice (that was in your original snippet).
  • Avoid integer division: if both sum and count are integers, cast one to double before dividing.
  • Defend against bad input with a simple cin check and cin.clear()/ignore() loop.

Example (reads n numbers, validates input, computes a floating-point average):

#include <iostream>
#include <vector>
#include <limits>
#include <iomanip>

int main() {
    std::cout << "How many numbers do you want to average? ";
    int n;
    if (!(std::cin >> n) || n <= 0) return 1; // validate count

    double sum = 0.0;
    for (int i = 0; i < n; ++i) {
        double v;
        std::cout << "Enter number " << (i + 1) << ": ";
        while (!(std::cin >> v)) { // robust input
            std::cin.clear();
            std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
            std::cout << "Please enter a valid number: ";
        }
        sum += v;
    }

    double average = sum / static_cast<double>(n);
    std::cout << std::fixed << std::setprecision(6) << "The average is " << average << '\n';
}

Troubleshooting notes: if results look truncated, check for integer division; if sums grow large, use long double or check input ranges; to save memory you can accumulate on the fly (as above) rather than storing all values.

  1. You never used NumCount. Thus you loop went infinitely.
  2. You never incremented total every time they entered a number.
  3. None of your variables were initialized so the values could be anything.
  4. If using codeblocks, right click anywhere in the source editor and press format A-Style. Then you can post it here. It's hard to read code that isn't indented.

    int main(int argc, char* argv[])
    {
        int numCount = 0, total = 0;
        double average;
        cout << "How many numbers do you want to average? ";
        cin>> numCount;
        for (int count = 0; count < numCount; count++)
        {
            int num;
            cout << "Enter a number: ";
            cin >> num;
            total += num;
        }
        average = total / numCount;
        cout << "The average is " <<average<<endl;
    
        cin.get();
        return 0;
    }
    

Edit: WTH? Something is wrong with Daniweb's code parser :S So you have to parse the code twice. At least for me that's how the indentation stayed. I think it's a glitch.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.