Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > An Example from Accelerated C++ with question

Reply
Thread Tools

An Example from Accelerated C++ with question

 
 
Lambda
Guest
Posts: n/a
 
      11-18-2007
There is a student grade example in the book Accelerated C++ chapter
4.
Related code is:

main.cc:
// read and store all the students' data.
// Invariant: `students' contains all the student records read so far
// `maxlen' contains the length of the longest name in `students'
while (read(cin, record)) {
// find length of longest name
maxlen = max(maxlen, record.name.size());
students.push_back(record);
}

istream& read(istream& is, Student_info& s)
{
// read and store the student's name and midterm and final exam
grades
is >> s.name >> s.midterm >> s.final;

read_hw(is, s.homework); // read and store all the student's
homework grades
return is;
}

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

I make a file with two lines:
John 90 80 60 78 95
Smith 93 76 40 80 60

And feed it to the program.
the istream& read(istream& is, Student_info& s)
function will read all the name and grade and put them in a
Student_info structure.

My question is how does the program know I have read all the data from
the 2 lines file.
the
istream& read_hw(istream& in, vector<double>& hw)
function will do in.clear();
so even cin encounter EOF, the eofbit will be cleared.

I think the main function will try to read the non exist third line
and fail.
But actually the program works! How does the code know it has read all
the data??
 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      11-18-2007
Lambda <(E-Mail Removed)> wrote:

> My question is how does the program know I have read all the data
> from the 2 lines file. the istream& read_hw(istream& in,
> vector<double>& hw) function will do in.clear(); so even cin
> encounter EOF, the eofbit will be cleared.


Not quite...

> I think the main function will try to read the non exist third line
> and fail. But actually the program works! How does the code know it
> has read all the data??


Step through the program by hand, assume that the file is empty. What
will happen?

1: while (read(cin, record)) {
2: (inside read) is >> s.name >> s.midterm >> s.final;
At this point the eof is encounterd.
3: read_hw(is, s.homework);
4: (inside read_hw) if (in) {
'in' returns 'false' in this case so the entire block is passed up.
I.E., in.clear() isn't called.
5: return in;
6: (back inside read) return is;
7: back to (1) the while loop is exited.

Hope the above helps you understand the code, and gives you a tool to
understand other code in the future.

When I started programing (back in the stone ages. I didn't have
access to a mainframe so I had to step through my code by hand to see if
it worked. I still use the method today.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
"Accelerated C++" beginner question. TonyJeffs C++ 39 05-02-2005 05:26 AM
"Accelerated" certification testing programs Jameson D MCSE 2 11-26-2004 01:47 PM
question about excerpt from "Accelerated C++" Joe Laughlin C++ 4 10-19-2004 04:41 PM
Accelerated C++ book outdated? Markus Dreyer C++ 35 01-28-2004 09:28 AM
Accelerated C++ Sample Solutions Martin C++ 8 10-23-2003 05:32 PM



Advertisments