Writing to a .txt file using a function

Hello!
So I'm trying to figure out how to pass a txt file to a function to be written to only. I will not need to read from it. I cannot figure out how to set up the paramters as I keep getting all kind of complicated complier errors and do not know how to decode them. Its a class project for a payroll input and the output needs to all be sent to a .txt file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void printReportHeadings(whatgoeshere? * fileName); //prototype
int main ()
{
ofstream report;
report.open ("report.txt"); //creates and opens output file called Report.txt
printReportHeadings(again what goes in here?); // function call
report.close(); //closes Report.txt file
return 0;
}
void printReportHeadings(ostream &report) // <---are the paramters correct for the function definition?
{
	
	report << "\n   Employee         Pay     Reg Hours    Gross    Fed Tax     SSI Tax    Net";
	report << "\n   Name             Rate    Ovt Hours    Pay      State Tax   Defr       Pay";
	report << "\n   =========        =====   =========   =======   ========    =======   ======";
}

See my notes after the function proto, call, and definition.

(hopefully the formatting holds using the code tags...first time poster)
I've tried to search for an answer to my problem and haven't been able to come up with a solution yet. Thank you for any help!
Last edited on
Er, well, first of all, did you try making your function prototype and your function definition match?
1
2
3
4
5
6
void printReportHeadings(ostream &report);
[..]
void printReportHeadings(ostream &report)
{
	[..]
}


And you'd just call the function by doing printReportHeadings(report); in main

If you still need help beyond this I can paste the exact code that works, but honestly it's basically just the sum of the changes I listed.
Last edited on
prototype and function definition both exactly same.

so
protoype=void printReportHeadings(ostream& report);
definition=void printReportHeadings(ostream& report) {}

function call is without any type name just variables name and function name so it will be
function call=printReportHeadings(report)
OK thank you but still not working.
Here is the exact stub code I'm trying to run.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>

void printReportHeadings(ostream &report); //3.1 print report headings
using namespace std;

int main()
{
	ofstream report;
	report.open ("report.txt"); //creates and opens output file called Report.txt
	printReportHeadings(report);
	return 0;
}

void printReportHeadings(ostream &report)
{
	
	report << "\n   Employee         Pay     Reg Hours    Gross    Fed Tax     SSI Tax    Net";
	report << "\n   Name             Rate    Ovt Hours    Pay      State Tax   Defr       Pay";
	report << "\n   =========        =====   =========   =======   ========    =======   ======";
}


Also I will eventually have another funtion writing to this same file within a loop as these are just the file headers. Thanks again!

errors:
'ostream' : undeclared identifier
'report' : undeclared identifier
'printReportHeadings' : illegal use of type 'void'
sorry for the wrong guidance

for file streaming you should use fstream classes, for output to the file use ofstream and input to the file use ifstream.
ostream and istream is for output and input to and from the screen.

so you should change

ostream to ofstream in the function prototype and definiition
and declare your prototype before main and after using namespace std;
Thank you! It worked! Very much appreciated, now i can begin writing the rest of it.
The reason you had trouble is because you put using namespace std; after your function prototype, so it didn't know what an "ostream" was at that point. Either way, good luck :)
Last edited on
Topic archived. No new replies allowed.