Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Plebeian Tier Question

Name: Anonymous 2012-04-30 19:39

First semester CS major here - I was absent from class once a while back and now that we're referencing the material I missed, I'm a little clueless. I have no friends so I figured I'd ask you guys.

Basically put, I'm having a little trouble how ifstream objects work. Before this segment of code, I've successfully opened a file and assigned it to ifstream variable "infile," but I don't know how I'm supposed to use it.

for (i = 1; i<= 3; i++)
{
    getInputs (infile, l, w);
    a = getArea (l, w);
    displayArea (l, w, a);
}

I'm fine with the functions displayArea and getArea, but this is what I'm working off of with getInputs and I'm a little stumped.

getInputs (ifstream& inputFile, double& length, double& width)
{
    inputFile >> length >> width;
}

I realize this fragment is probably retarded but I'm clueless - I'd appreciate any help you can give me.

Name: Anonymous 2012-05-01 13:32

>>4
To get a clue about what the >> operator does in case of ifstream, check the documentation. Since this is C++ you can assume from the beginning that what you see will be absolutely retarded.

ifstream& operator>>(ifstream&, some type here&);

What it does is read from the ifstream, in a type-specific way, into the second argument. The return value is the changed ifstream (which is actually just the same ifstream with some changed state). The reason for the return value is simply for making reading chainable.

So (inputFile >> length) return an ifstream which then has operator>> called on it with `width' as an argument.

By now you should be puking blood

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List