Math related calculation error?
Ok, so the issue I am now having with this program is that the average calculation is incorrect. However, the equation itself is so simple it seems unlikely that I could be so bone-headed as to mess that up. So, I am left thinking it has something to do with placement inside the if statements.
Sample input/output: enter 1, 3, the break loop with control + z. This should give an average of 2 but instead gives an average of 2.33333.
The other problem I have is when the user breaks the loop before entering a number. It gives the "out of range, ignored" error before the "No data was entered" error. The first error should not occur and only happens when control+Z is used before any numbers.
Code:
/*
int main ()
{
double number, tot, counter, average, range, low, high;
counter = 1.0;
tot = 0.0;
low = 100.0;
high = 0.0;
while (!cin.eof())
{
cin >> number;
if ((number >= 0.0) && (number <= 100.0))
{
counter = counter + 1.0;
tot = tot + number;
if (number < low)
{
low = number;
}
if (number > high)
{
high = number;
}
}
if (number < 0.0 || number > 100.0)
{
cout << "out of range; ignored." << endl;
}
}
if (counter == 0.0)
{
cout << "No data was entered" << endl;
}
else
{
average = tot / counter;
cout << "The average is " << average << endl;
range = high - low;
cout << "The range is " << range << endl;
}
}