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