473,472 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Probably a Stupid Mistake

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!
Jul 22 '05 #1
6 1726
On Fri, 22 Oct 2004 00:06:11 +0100, Materialised wrote:
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
Jul 22 '05 #2
Jay Nabonne wrote:
<snip>
Thanks for any help :)

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.
Jul 22 '05 #3
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.
Jul 22 '05 #4
Materialised wrote:

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 <stdexcept>
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, vector<double>& hw)
{
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,
--
Ben M.
Jul 22 '05 #5

"Ben Measures" <sa****************@removehotmail.com> wrote in message
news:ya******************@text.news.blueyonder.co. uk...
| 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
Jul 22 '05 #6
Chris ( Val ) wrote:

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.

--
Ben M.
Jul 22 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: John Wiese | last post by:
I am creating my first website and need some help desperatley. I have choosen to use Front Page XP for this simple Intranet project. All I am trying to do is: 1. Collect childrens names for a...
3
by: sieg1974 | last post by:
Hi, this must be a stupid mistake, but I can't find it. When I try to link the following code: #include <iostream.h> #include "TestSK.hh" class Test_Impl : public POA_Test // , public...
16
by: Justin Hoffman | last post by:
This is a question concerning query optimisation. Sorry if it's a bit long, but thanks to anyone who has the patience to help - This is my first post here... If I have two tables:...
3
by: mirko | last post by:
Hello, I have a problem with my include_path and I don't know why... Can anybody see the mistake? my configuration: PHP Version 4.3.11 System: Windows NT 5.0 build 2195
36
by: Hoopster | last post by:
Hello, I know nothing about C++ but want to get started. Is there any good free C++ program that I can try to see if I like programming? I also need a good free compiler. I don't want to...
8
by: Michael | last post by:
Hello everyone here, I've got a question according to a C program, but I'm not sure if this one is "off-topic". So, its a program using two structures each containing an array (a.array1...
3
by: Catweasel | last post by:
I'm new to C++ and have been chucked in at the deep-end. I have a C++ console app that works fine. All I want to do is write to file however as soon as I include the fstream library and try to...
1
by: Stef Mientki | last post by:
Does anyone know the equivalent of the MatLab "diff" function. The "diff" functions calculates the difference between 2 succeeding elements of an array. I need to detect (fast) the falling edge of...
5
by: Puppet_Sock | last post by:
So, I'm madly coding away, and my fingers stutter, and I produce this. (mfirstToken is a std::string object.) if(m_firstToken.c_str() == 'M' || m_firstToken.c_str().c_str() == 'T') { // ......
0
by: webandwe | last post by:
Hi, When I press sign(submit) - It looks like something is wrong and the code does not want to insert the info into my SQL...I get the error message "could not query.." that I type in under...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.