Hello all, so I am doing an exercise from a book, and I am suppose to take multiple students name, and their final grades when computed into two separate vectors. I am unsure if my program even works at all because of the usage of precision. I think I want to round the grades up to about 3 decimal places. However the book I am using briefly describes how to use precision. Can someone please point me in the right direction on using the precision method?
Code:
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
using std::cin; using std::setprecision;
using std::cout; using std::string;
using std::endl; using std::streamsize;
using std::vector;
int main(){
vector<string> students;
vector<int> grades;
string state;
for(int i = 0; state != "DONE"; i++){
// ask for and read the student's name
cout << "Please enter student's first name: ";
string name;
cin >> name;
students.push_back(name);
// ask for and read the midterm and final grades
cout << "Please enter midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
// ask for the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file if done,";
// the number and sum of grades read so far
int count = 0;
double sum = 0;
// a variable into which to read
double x;
// invariant:
// we have read count grades so far, and
// sum is the sum of the first count grades
while (cin >> x) {
++count;
sum += x;
}
//PROBLEM HERE
//adds final grade to the vector
int grade = setprecision(3) << 0.2 * midterm + 0.4 * final + 0.4 * sum / count
grades.push_back(grade);
cout << "DONE?" ;
cin >> state;
}
// write the result
cout << "Your final grades are: " << endl;
for(int i = 0; i != students.size(); i++){
cout << students[i]
<< grades[i] << endl;
}
return 0;
}