avg in while loop

i am getting 8.66 as my answer but i am suppose to get 8.33.
input 10 30 -15 0
not sure where my error is. can you help me please.

// This program is suppose to count the integers than average them.
// with zero being the end input.

#include <iostream>
using namespace std;

int main()
{
int num = 1; // the integers entered by the user
int amt = 0; // amount of numbers entered
float sum = 1; // the amount of numbers added and new sum after adding number
double avg; // the average of all the numbers


// display the while statement
while (num != 0)
{
cout << "Get a integer from the user.\n";
cin >> num;

sum = sum + num;
amt++;
}

// the equation for the average
avg = sum /(amt-1);


cout << " You have entered " << amt << " numbers.";
cout << " The average is " << avg << endl;
return 0;
}
Please use [code][/code] tags.

You should initialize sum to 0, not 1. You may also want to check for division by 0.

Hope that helps.
Topic archived. No new replies allowed.