How to work with data read in via loop using multiple functions

I am in the design stage of a program and must be missing the boat on something fundamental. I am hoping someone can help identify where I am going wrong in my thinking and offer a prompt to help redirect my approach.

The program is supposed to read in 7 rows of data from a file, perform a sum calculation on some of the data, and write a report to file containing headers, all of the data from the source file, and the calculated sum. Lastly, the program must contain at least two functions other than main.

This is my first ever programming class. I have successfully created functions before and understand the basics of reading from a file and writing to a file. What I think is tripping me up about this program is the read in of multiple rows of data containing multiple data points on each row and trying to mesh this with multiple functions.

I was able to produce the desired program outcome using a single function. I used a loop to read in the data, update the sum, and write the row data to a file. Other portions of the report - headers and total - are written outside of the loop. I don't know if reading in the data via a loop is correct. If it is, then I am lost on how to use multiple functions without requiring the need to read in the data again - which seems like the wrong direction.

I apologize this isn't more concise. I appreciate any hints that might point me in the right direction as I think about a reasonable way to design the program using multiple functions.
I was where you are not too long ago, so I understand your frustration. The teacher is getting you comfortable with creating functions to do all the work for you in main().

Could you post the code you have now? I bet we (YOU) could take all you've done in main and break it up into functions to make main() no more than a few lines.

For instance, you could use a function called getData(string& stringname) to read in all the data in that for loop. To answer your question, yes, using a for loop is the best way I know of to get parsed, sequential data from a text file.
Last edited on
Thank you for the response and empathy - made me feel a bit better right off the bat. I am embarrassed to say that I don't know how to copy code out of the editor program to paste in here.

I haven't worked with the getData functionality beyond some basic understanding. I'll revert to my textbook and see if I can make sense of it.

I'm also not sure if posting my code would put me in violation of school policy (I'm trying to find out), but in general and brief, my code looks as follows this paragraph. If you'd like to turn your attentions to others in need of assistance, I just received word from my professor that the proposed design will be posted shortly (I went ahead and submitted my design even though I knew it was not meeting program specs because it was due), so, hopefully that will help shed some additional light on my confusion and point me in the right direction given where we are in the class.

I really cannot thank you enough for the response you have provided, though; thank you!!

int num1, num2, num3, num4, num5

fin.open("filename");
fout.open ("otherfilename");

fout << "headers and such <<;
{
for (day = 1; day <8; day++)
{
fin >> num1 >> num2 >> num3 >> num4;
fout << num1 << num2 << num3 << num4;
num5 = num5 + num4;
}
fin.close();
fout << num5;
}
fout.close();
return 0;
You're welcome. You should know that getData() is not a standard function, but one I was saying you could create. Based on the code you provided, for instance:

**FUNCTION** THIS GOES ABOVE MAIN (did you know main() is a function as well?)
1
2
3
4
5
void getData(string& num1, string& num2, string& num3, string& num4, ifstream& fin)
 // NOTE: '&' means you wish to pass by reference (return/modify their values)
{
fin >> num1 >> num2 >> num3 >> num4; // look familiar?
}


now replace (in function main())
fin >> num1 >> num2 >> num3 >> num4;



with your new FUNCTION CALL!! :)
getData(num1, num2, num3, fin) <---**FUNCTION CALL**

You just created a getData() function that takes four strings and an ifstream by reference.


Oh, he wants you to have two functions? How about...
writeData(string& num1, string& num2, string& num3, string& num4, ofstream fout) ?
Bet you can do it. Good luck!
Last edited on
Thank you very much!!
Topic archived. No new replies allowed.