You are possibly loosing some decimals. Change from
Quote:
return (avg = (sum/4));
to
return (avg = ((float)sum/4));
It is worthy noting your code could be more concise, e.g.
#include <iostream>
using namespace std;
class Average
{
int sum, items;
public:
Average():sum(0), items(0){}
void add(int v){ sum += v; ++items; }
double avg()
{
if (items==0) throw("sorry, no items");
return (double)sum/items;
}
};
int main()
{
int marks;
Average a;
while (true)
{
cout << "plaese input marks (-1 to stop): " << endl;
cin >> marks;
if ( marks < 0) break;
a.add(marks);
};
try
{
cout << "the average is " << a.avg() << endl;
}
catch( const char * msg )
{
cout << msg << endl;
}
}