Reputation: 23
I tried everything and what I have is something I know is incorrect but at least the output is formatted how I need it for one of the two files. I need to send information out into two separate .txt files that both carry different information. How do I do that using the current array functions I already have. I have spent hours trying to figure this out and now its up to you all! Thank you.
Main-
#include<iostream>
#include<fstream>
#include<string>
#include "Payroll.h"
using namespace std;
const int NUM_EMPLOYEE = 75;
int main()
{
int dependents;
double payrate;
string name;
double hours;
ifstream fin;
int count = 0;
Payroll employeeArray[NUM_EMPLOYEE];
fin.open("employeeData.txt");
if (!fin)
{
cout << "Error opening data file\n";
return 0;
}
else
{
while(fin >> payrate >> dependents)
{
getline(fin, name);
employeeArray[count].setWage(payrate);
employeeArray[count].setDependents(dependents);
employeeArray[count].setName(name);
cout << "How many hours has" << name << " worked? ";
cin >> hours;
employeeArray[count].setHours(hours);
count++;
}
}
for (int i = 0; i < count; i++)
{
employeeArray[i].printPayDetails(cout << endl);
}
cout << endl;
return 0;
}
Print Function-
void Payroll::printPayDetails(ostream& out)
{
double normPay = getNormPay();
double overTime = getOverPay();
double grossPay = getGrossPay();
double taxAmount = getTaxRate();
double netPay = computePay();
const int SIZE = 9;
out << fixed << setprecision(2) << right << setw(5) << hours << setw(SIZE) << normPay << setw(SIZE) << overTime ;
out << setw(SIZE) << grossPay << setw(SIZE) << taxAmount <<setw(SIZE) << netPay;
}
Upvotes: 1
Views: 1217
Reputation: 321
Your question wording is a bit shaky but I think I get what you're saying. If you want to output to two different file you're going to need two string streams. Below is an example:
#include <fstream>
void main()
{
//Open file 1
ofstream file1;
file1.open("file1.txt");
file1 << "Writing stuff to file 1!";
//Open file 2
ofstream file2;
file2.open("file2.txt");
file2 << "Writing stuff to file 2!";
//That the files are open you can pass them as arguments to the rest of your functions.
//Remember to use &
//At the end of your program remember to close the files
file1.close();
file2.close();
}
Upvotes: 2