using >> to print to a file from a class member function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void Employee::printPayRollReport(){
    ofstream payRollReport;
    payRollReport << setw(5) << id;
    payRollReport << setw(20) << name;
}

void Paycheck::printStruct(){
    payRollReport << "$ " << setw(10) << fixed << setprecision(2)  << grossPay;
    payRollReport << "$ " << setw(10) << tax;
    payRollReport << "$ " << setw(10) << insurance;
    payRollReport << "$ " << setw(10) << netPay;
    payRollReport << endl;
}

    employees[index].printPayRollReport();
    paychecks[index].printStruct();


These are not printing and I'm not sure why. The functions are part of a Class and Struct respectively. The calls for the function are in main and in-between opening and closing the payRollReport.txt file. The column headers are printing, so the file is opening. Anyway, PLEASE help! Thank you.
Your printing is probably ok, but in printPayRollReport you don't ever open any file. You just make an ofstream and never open anything.

With printStruct you don't even have an ofstream so all those lines are invalid.
I open and close it in the main file, though on further thought I guess that it's not the same variable. If I do it in the function, it overwrites itself leaving only the last entry from the struct. Is there a way to get around this?
Never mind I remember how, thank you!
Oh and the ofstream for the struct is in inside the struct declaration.
Topic archived. No new replies allowed.