M
Materialised
Hi, could someone have a look at the following code and the error
message below, and point out where my error is, as I am having trouble
figuring where I am going wrong.
#include <algorithm>
#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::sort; 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];
}
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// Compute a students overall grade from midterm and final exam grades
// and vector of homework grades
// this function does not sort its argument because median does so for us
double grade(double midterm, double final, const vector<double>& hw)
{
if(hw.size() == 0)
throw_domain_error("Student has doen no homework");
return grade(midterm, final, median(hw));
}
//read homework grades from a 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);
//clean the stream so input will work for the next student
in.clear();
}
return in;
}
int main()
{
//ask for the students name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
//ask for and read 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: ";
// read the homework grades
vector<double> homework;
read_hw(cin, homework);
//compute and generate the final grade if possible
try {
double final_grade = grade(midterm, final, homework);
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< final_grade << setprecision(prec) << endl;
} catch (domain_error) {
cout << endl << "You must enter your grades. "
"Please try again." << endl;
return 1;
}
return 0;
}
Here is the error g++ gives when I try to compile:
grades7.cpp: In function `double median(std::vector<double,
std::allocator<double> >)':
grades7.cpp:19: error: `throw_domain_error' undeclared (first use this
function)
grades7.cpp:19: error: (Each undeclared identifier is reported only once for
each function it appears in.)
grades7.cpp:20: error: no matching function for call to `sort(
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, <unknown type>)'
grades7.cpp: In function `double grade(double, double, const
std::vector<double, std::allocator<double> >&)':
grades7.cpp:35: error: `throw_domain_error' undeclared (first use this
function)
grades7.cpp: At global scope:
grades7.cpp:39: error: syntax error before `&' token
grades7.cpp:46: error: syntax error before `while'
Thanks for any help
--
perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
107, 'er', 64, 46, 10;'
Marge: Homer! There's someone here who can help you...
Homer: Is it Batman?
Marge: No, he's a scientist.
Homer: Batman's a scientist?!
Marge: It's not Batman!
message below, and point out where my error is, as I am having trouble
figuring where I am going wrong.
#include <algorithm>
#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::sort; 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];
}
double grade(double midterm, double final, double homework)
{
return 0.2 * midterm + 0.4 * final + 0.4 * homework;
}
// Compute a students overall grade from midterm and final exam grades
// and vector of homework grades
// this function does not sort its argument because median does so for us
double grade(double midterm, double final, const vector<double>& hw)
{
if(hw.size() == 0)
throw_domain_error("Student has doen no homework");
return grade(midterm, final, median(hw));
}
//read homework grades from a 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);
//clean the stream so input will work for the next student
in.clear();
}
return in;
}
int main()
{
//ask for the students name
cout << "Please enter your first name: ";
string name;
cin >> name;
cout << "Hello, " << name << "!" << endl;
//ask for and read 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: ";
// read the homework grades
vector<double> homework;
read_hw(cin, homework);
//compute and generate the final grade if possible
try {
double final_grade = grade(midterm, final, homework);
streamsize prec = cout.precision();
cout << "Your final grade is " << setprecision(3)
<< final_grade << setprecision(prec) << endl;
} catch (domain_error) {
cout << endl << "You must enter your grades. "
"Please try again." << endl;
return 1;
}
return 0;
}
Here is the error g++ gives when I try to compile:
grades7.cpp: In function `double median(std::vector<double,
std::allocator<double> >)':
grades7.cpp:19: error: `throw_domain_error' undeclared (first use this
function)
grades7.cpp:19: error: (Each undeclared identifier is reported only once for
each function it appears in.)
grades7.cpp:20: error: no matching function for call to `sort(
__gnu_cxx::__normal_iterator<double*, std::vector<double,
std::allocator<double> > >, <unknown type>)'
grades7.cpp: In function `double grade(double, double, const
std::vector<double, std::allocator<double> >&)':
grades7.cpp:35: error: `throw_domain_error' undeclared (first use this
function)
grades7.cpp: At global scope:
grades7.cpp:39: error: syntax error before `&' token
grades7.cpp:46: error: syntax error before `while'
Thanks for any help
--
perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
107, 'er', 64, 46, 10;'
Marge: Homer! There's someone here who can help you...
Homer: Is it Batman?
Marge: No, he's a scientist.
Homer: Batman's a scientist?!
Marge: It's not Batman!