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;
}
  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.