I'm trying to do this exercise from the book accelerated c++ by Koeing & Moo (chapter 3), which basically it should calculate the average of the grades midterm, final & homework.... The problem is that the answer i'm getting is only the sum of the grades thus i think that i'm missing something regarding the count... any ideas please ??
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
usingnamespace std;
int main ()
{ cout << "Please enter your Midterm and Final grades : ";
double midterm, final;
cin >> midterm >> final;
cout << "Please enter your Homework grades, followed by end of file: " << endl;
int count = 0;
double sum = 0;
double x;
while ( cin >> x )
{ ++count;
sum += x; }
streamsize prec = cout.precision(3);
cout << "Your final grade is " << (midterm + final + sum) / count << endl;
cout.precision(prec);
return 0;
}
It seems to work for me on Linux but you should avoid doing this: usingnamespace std; It pollutes the global namespace and introduces a lot of symbols that you don't expect to be defined. Two of which are sum and count. So to avoid hard to find conflicts it is highly recommended to stop doing usingnamespace std;.
Actually, I am not convinced by your math. You only count the number of homework assignments and do not take the first two results into consideration in the count. So I think your count should begin at 2 to account for the first two grades input.
I doubt that it works as intended. The total grade is made up 1/3 each by midterm, final and homework.
The total homework grade consists of the average grade of the individual homework grades.