Notes, Tuesday, October 1


Previous page | Next page


Input operators for Assignment 3

The low-level input (for a single entry) can be done like this:
istream& operator >> (istream& in, CatalogEntry& entry)
{
    string discard;

    getline (in, discard, '"');
    getline (in, entry.author, '"');
    getline (in, discard, '"');
    getline (in, entry.title, '"');
    getline (in, discard, '"');
    getline (in, entry.callNumber, '"');
    getline (in, discard);

    return in;
}
(getline is described in the textbook on page 770.)

This then gets used in the high-level input operator by reading until the input stream has been consumed ...

istream& operator >> (istream& in, Catalog& cat)
{
    CatalogEntry entry;

    while (in.peek () != EOF) {
        in >> entry;
        cat += entry;
    }

    return in;
}
This is used in the main program like
    infile >> catalog;
(as discussed last time).


Previous page | Next page | Back to top

3:40 PM, Thursday, December 12, 2002