Probably a Stupid Mistake

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!
 
J

Jay Nabonne

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;

// Add, for below
using std::istream;
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");

// Should be:
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end);

// Should be:
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");

// Should be:
throw domain_error("Student has done 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 :)

HTH

- Jay
 
M

Materialised

Jay Nabonne wrote:
HTH

- Jay

Thanks Jay
--
perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
107, 'er', 64, 46, 10;'

Mr.Burns: Quick Smithers. Bring the mind eraser device!
Smithers:You mean the revolver, sir?
Mr.Burns: Precisely.
 
M

Materialised

I know it is bad form to reply to ones self, but I also missed:
#include <stdexcept>

--
perl -e 'printf "%silto%c%sal%c%s%ccodegurus%corg%c", "ma", 58, "mw",
107, 'er', 64, 46, 10;'

Homer: [drunk] Look, the thing about my family is there's five of us.
Marge, Bart, Girl Bart, the one who doesn't talk, and the fat guy. How I
loathe him.
 
B

Ben Measures

Materialised said:
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>

// You need to include the following to throw a std::domain_error.
#include said:
using std::cin; using std::setprecision;
using std::cout; using std::string;
using std::endl; using std::streamsize;
using std::sort; using std::vector;

// Indicate that you're using domain_error from the std:: namespace.
using std::domain_error;

// Similarly,
using std::istream;
double median(vector<double> vec)
{
typedef vector<double>::size_type vec_sz;

vec_sz size = vec.size();
if(size == 0)

// Style. No error here but I like to add a space so it's clear 'if' is
// a keyword and not a function.
if (size == 0)
throw_domain_error("median of an empty vector");

// You need to throw a domain_error. The keyword is throw and the object
// is domain_error - they are separate.
throw domain_error("median of an empty vector");
sort(vec.begin(), vec.end);

// Typo, should be:
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)

// Style.
if (hw.size() == 0)
throw_domain_error("Student has doen no homework");

// Error.
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)

// Style. Keep the & with the type.
istream& read_hw(istream& in said:
{
if(in) {

// Style.
if (in) {
//get rid of previous contents
hw.clear();
//read homework grades
double x;
while(in >> x)

// Style.
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;
}

Summary: 6 errors and 5 style issues.

Hth,
 
C

Chris \( Val \)

| Materialised wrote:

[snip]

| // Style.
| if (in) {
|
| > //get rid of previous contents
| > hw.clear();
| > //read homework grades
| > double x;
| > while(in >> x)
|
| // Style.
| while (in >> x)

[snip]

While I can appreciate that you and others might prefer
this style (using an space before the opening parenthesis),
there is no way that these identifiers could possibly be
mistaken for user defined functions in either 'C' or 'C++',
because they are common (lowercase) keywords to both languages.

They're just plain 'ol' conditional and iterative statements,
and I would be surprised if anyone got into trouble over these
particular identifiers, because they are self documenting by
nature of their names, in any language really.

Don't you know that style issues lead to flamewars ? :)

Cheers.
Chris Val
 
B

Ben Measures

Chris said:
Don't you know that style issues lead to flamewars ? :)

Perhaps. However, as a newbie I welcomed suggestions for improving my
programming style, even if I didn't choose to follow them all.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top