I am new to c++ programming and I started with
as beginning book as it was recommended in this forum. The book was going ok until I reached chapter 4.1
I am not able to compile the code even I have tried copy paste. Don't know what's going wrong.
I am using dev-c++ compiler 4.9.
here is the compiler message-
C:\c\grade.cpp In function `double median(std::vector<double, std::allocator<double> >)':
20 C:\c\grade.cpp `domain_error' undeclared (first use this function)
(Each undeclared identifier is reported only once for each function it appears in.)
C:\c\grade.cpp In function `double grade(double, double, const std::vector<double, std::allocator<double> >&)':
37 C:\c\grade.cpp `domain_error' undeclared (first use this function)
37 C:\c\grade.cpp At global scope:
44 C:\c\grade.cpp expected constructor, destructor, or type conversion before '&' token
44 C:\c\grade.cpp expected `,' or `;' before '&' token
Here is the code.
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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
|
#include <algorithm>
#include <iomanip>
#include <ios>
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
using std::cin; using std::sort;
using std::cout; using std::streamsize;
using std::endl; using std::string;
using std::setprecision; using std::vector;
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;
vec_sz size = vec.size();
if (size == 0)
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end());
vec_sz mid = size / 2;
return size % 2 == 0 ? (vec[mid] + vec[mid-1]) / 2 : vec[mid];
}
// compute a student's overall grade from midterm and final exam grades and homework grade
double grade(double midterm, double final, double homework)
{
return midterm*0.2+final*0.4+homework*0.4;
}
double grade(double midterm, double final, const vector<double>& hw)
{
if (hw.size() == 0)
throw domain_error("student has done no homework");
return grade(midterm, final, median(hw));
}
// read homework grades from an input stream into a vector<double>
istream& read_hw(istream& in, vector<double>& hw)
{
if (in) {
// get rid of previous contents
hw.clear() ;
// read homework grades
double x;
while (in >> x)
hw.push_back(x);
// clear the stream so that input will work for the next student
in.clear();
}
return in;
}
int main()
{
// ask for and read the student's 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 the homework grades
cout << "Enter all your homework grades, "
"followed by end-of-file: ";
vector<double> homework;
// read the homework grades
if (cin) {
// get rid of previous contents
homework.clear() ;
// read homework grades
double x;
while (cin >> x)
homework.push_back(x);
// clear the stream so that input will work for the next student
cin.clear();
}
// compute and generate the final grade, if possible
double final_grade = grade(midterm, final, homework);
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< final_grade << setprecision(prec) << endl;
return 0;
}
| |
So any help will be appreciated. As you guys are experienced c++ programmer and I am totally novice so please excuse me if I am making any silly mistake.
Thanx