An Example from Accelerated C++ with question

L

Lambda

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??
 
D

Daniel T.

Lambda said:
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.
 

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

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top