### AVerage help

I'm trying to get the average of 3 test scores in the code below but it won't work, how do I go about doing it?

 ``1234567891011`` `````` friend std::ostream &operator<<(std::ostream &out, const Grade &s) { return out << "Name: " << s.first << " " << s.middle << " " << s.last << std::endl << "test1: " << s.test1 << std::endl << "test2: " << s.test2 << std::endl << "test3: " << s.test3 << std::endl << "average: " << s.test1+s.test2+s.test3/3 << std::endl << "Absences: " << s.absence << std::endl; } }; ``````
You have to put parenthesis, same as in math.
`(s.test1+s.test2+s.test3)/3`
ok i do that and i get this error, heres my code:

error C2676: binary '/' : 'std::basic_string<_Elem,_Traits,_Ax>' does not define this operator or a conversion to a type acceptable to the predefined operator

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051`` ``````#include #include #include #include #include using namespace std; struct Grade { std::string last, first, middle, test1, test2, test3, absence; friend std::istream &operator>>(std::istream &in, Grade &s) { return in >> s.last >> s.first >> s.middle >> s.test1 >> s.test2 >> s.test3 >> s.absence; } friend std::ostream &operator<<(std::ostream &out, const Grade &s) { return out << "Name: " << s.first << " " << s.middle << " " << s.last << std::endl << "test1: " << s.test1 << std::endl << "test2: " << s.test2 << std::endl << "test3: " << s.test3 << std::endl << "average: " << (s.test1+s.test2+s.test3)/3 << std::endl << "Absences: " << s.absence << std::endl; } }; void main () { std::vector grades; { std::ifstream infile ("students.txt"); Grade temp; while(infile >> temp) { grades.push_back(temp); } } for(size_t i = 0; i < grades.size(); ++i) { std::cout << grades[i] << std::endl; } }``````
The test variables are strings. You can't divide a string. You probably want to make them some numerical type instead of string.
How?
 ``12345678910`` ``````struct Grade { std::string last, first, middle, absence; double test1, test2, test3; ...``````

Last edited on
Topic archived. No new replies allowed.