I'm working my way through Accelerated C++ and I've come across this exercise. The read_hw function works fine if it's void as long as I comment out the "return in" line. The author didn't bother to explain why he's returning a reference to the cin that was passed in the first place. Also I'm not sure what to make of the if (in) here, since it was passed as a parameter it seems like it should be there. I would greatly appreciate any (correct) explanation for almost anything going on here related to istream/cin.

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

int main()
{
    vector<double> homework;
    read_hw(cin, homework);
    return 0;
}

the reason he has it returning the an istream reference is to be able to chain it. by that I mean

cin >> input >> anotherInput >> read_hw(cin, homework);

disclaimer: this is going to be really noobish.
so i put together this:

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

istream& read_hw(istream& in, vector<double>& hw)
{
    if (in)
    {
        // get rid of previous contents
        hw.clear();
        // read homework grade
        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;
}

int main()
{
    double a, b;
    vector<double> homework;
    cin >> a >> b >> read_hw(cin, homework);
    system("pause"); //bad form, i know
    return 0;
}

and the error i get is:
ambiguous load for 'operator>>' etc.
thoughts? i'm clearly failing to grasp something very basic here.

>> cin >> a >> b >> read_hw(cin, homework);

that essentially turns into cin >> cin which doesn't make sense. I agree, for your function, read_hw you should make it void. Enabling syntax such as read_hw(cin,homework) >> a >> b is obfuscated.

Just think read_hw as a regular function and call it as a regular function in its own statement. Don't try to use the returned value as there is no need. A better approach would be this :

#include <iostream>
#include <cstdlib>
#include <iostream>
#include <vector>

using namespace std;

istream& operator >>(istream& in, vector<double>& hw)
{
    if (in)
    {
        // get rid of previous contents
        hw.clear();
        // read homework grade
        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;
}

int main()
{
    double a, b;
    vector<double> homework;
    cin >> a >> b >> homework;

    cin.get();
    return 0;
}

>> cin >> a >> b >> read_hw(cin, homework);

that essentially turns into cin >> cin which doesn't make sense. I agree, for your function, read_hw you should make it void. Enabling syntax such as read_hw(cin,homework) >> a >> b is obfuscated.

Just think read_hw as a regular function and call it as a regular function in its own statement. Don't try to use the returned value as there is no need. A better approach would be this :
*code*

Thanks a lot. Guess I'll have to read more to figure out why he did it the way he did.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.