This was a fun project.
There is an issue with your math.
And you'll need one function from the <numeric> header.
Also, your I/O could use a little help, since you are requiring it to terminate to get grades. I'll give that to you for free:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int main()
{
cout << "What were your latest scores?\n";
vector <double> grades;
while (true)
{
cout << "Exam " << (grades.size() + 1) << "> ";
string s;
getline( cin, s );
if (s.empty()) break;
istringstream ss( s );
double temp;
ss >> temp;
if (!ss)
{
cout << "Er, try again or press ENTER to stop entering grades.\n";
continue;
}
grades.push_back( temp );
}
| |
For the remainder, some math:
The current grade is the average of all the scores, which is (sum of scores)/(number of scores).
Get the sum of scores using
double sum = accumulate( grades.begin(), grades.end(), 0 );
So the average is:
double current_grade = sum / grades.size();
Slick, huh?
Once you know that, tell the user what his current grade is before asking more questions.
Now you need to know two things:
- how many exams remain?
- what grade does the user want?
Now for the math:
desired = (sum + x) / (grades.size() + exams_remaining)
Solve for x.
Then the minimum grade required for each remaining exam is (x / exams_remaining).
Hope this helps.