An Example from Accelerated C++ with question

Discussion in 'C++' started by Lambda, Nov 18, 2007.

  1. Lambda

    Lambda Guest

    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??
    Lambda, Nov 18, 2007
    #1
    1. Advertising

  2. Lambda

    Daniel T. Guest

    Lambda <> 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.
    Daniel T., Nov 18, 2007
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Martin
    Replies:
    8
    Views:
    7,006
    Andrew Koenig
    Oct 23, 2003
  2. Markus Dreyer

    Accelerated C++ book outdated?

    Markus Dreyer, Jan 21, 2004, in forum: C++
    Replies:
    35
    Views:
    2,165
    Attila Feher
    Jan 28, 2004
  3. Steven T. Hatton
    Replies:
    5
    Views:
    369
    Steven T. Hatton
    Aug 27, 2004
  4. Joe Laughlin
    Replies:
    4
    Views:
    358
    Ron Natalie
    Oct 19, 2004
  5. TonyJeffs
    Replies:
    39
    Views:
    1,032
    Old Wolf
    May 2, 2005
Loading...

Share This Page