I am trying to create a program that computes a students grades and gives you the result. I am doing this as part of a task from a book called "Accelerated C++".
The problem I am encountering at the moment is that I enter mid term and final exam scores as well as homework scores and it seems to calculate the final grade. However it closes before I can read it. I tried adding a pause using cin.get(); at the end but it didn't work.
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
using std::setprecision;
using std::string;
using std::streamsize;
using std::vector;
using std::sort;
int main()
{
//ask for and read the students name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
//ask for and read the midterm and final grades
cout << "Please enter your midterm and final exam grades: ";
double midterm, final;
cin >> midterm >> final;
//Ask for their homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
vector<double> homework;
double x;
// Invariant: Homework contains all the homework grades read so far
while (cin >> x)
homework.push_back(x);
// Check that the student entered some homework grades
typedef vector<double>::size_type vec_sz;
vec_sz size = homework.size();
if (size == 0) {
cout << endl << "You must enter your grades. "
"Please try again." << endl;
return 1;
}
// Sort the grades
sort(homework.begin(), homework.end());
// Compute the median homework grade
vec_sz mid = size / 2;
double median;
median = size % 2 == 0 ? (homework[mid] + homework[mid - 1]) / 2
: homework[mid];
// compute and write the final grade
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< 0.2 * midterm + 0.4 * final + 0.4 * median
<< setprecision(prec) << endl;
cin.get();
return 0;
}
Is there a way to add a pause at the end so that I can see the result? Any help would be greatly appreciated. The code it exactly the same as the book. I just don't understand why it isn't working. Can anyone help?
Regards