I/O stream

Suppose I have a text document with lines as such:

4 3 2
6 1
2 4 3 3

Using I/O streams, I need to take the number that is by itself, multiply it by the sum of the numbers on the line below it, then output the answer.


1 ////////// 9
4 3 2///////
8 ////////// 40
1 4/////////

My question is: what I/O stream code will allow me to do this?
I should not need to use OOP to answer this either.

There is always one number on the odd-numbered lines of this text document, but a random number of numbers on even-numbered lines.

I should not need to use OOP to answer this either.

Yet std::ifstream is an object... Are you actually trying to write this in C for whatever reason? Because that will have an affect on our answer.

EDIT: Let me elaborate, your biggest obstacle is that the second line has a variable number of arguments and from assignments I've done myself I remember that teachers do not like it when you hard code the length of arrays when you don't need to. The simple solution to this is to use a dynamically sized container like a list but that would be an OOP approach which for some reason you do not want to use. We can work around this in the old school 'C' fashion but personally I'm better at C++.
Last edited on
It'd be for the best if it was in C, but I don't know how to solve it in either case; really, any approach will do for now.
Open the file.

If opened succesfully then loop through the values with the extraction operator (>>).

After reading the first value loop through the next values until you see a '\n' with peek(). While looping through these values calculate a running sum.

After exiting this inner loop output the first value * sum.
Topic archived. No new replies allowed.