I/O- Copying dataFile, functions, structs, pointers.

Hello all, my assignment is to write a program that will read 10 records into a dataFile. Also, I have to create a dataFile that has my full name, course name, & date on the first 3 lines. The program will open the output file in append mode & write your output to this file. When the execution of program is complete, your output file should contain your name, course name, and date followed by the 10 employee records including their total pay.
Each record should contain a employee ID, hours worked, and pay rate. You should then calculate the total pay and write the record to an output file.
Excluding the main function, the program should have 3 additional functions (get_cd, calc_cd, prt_cd), that will get the employee I'd, hours worked, pay rate, and calculate the total pay.
The pay should be calculated by multiplying the hours worked by pay rate plus 10% of the total pay for every 5 hours over 40.

Part of my assignment was to write a program before this one, which I have completed, and involved the same program except the portion of my name, course name, and date being written to the dataFile on the first 3 lines. I have successfully completed the 1st program, but I'm not sure what I'm leaving out on the 2nd program. When I compile it then the txt file with my name, course name, and date isn't copying to the text file on the first 3 lines. Here is a copy of my source code. Does anyone have any suggestions?


#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

using namespace std;

const int SIZE = 10;
struct payroll;
{
int employee_id;
double hr_worked;
double payrate;
double total_pay;
}

// function prototypes.
void get_cd(char *filename);
void calc_cd(struct payroll employees[], int size);
void prt_cd(struct payroll employees[], int size);

// define structure variable array
struct payroll admin[SIZE], office[SIZE], field[SIZE];

int main ()
{

// read data.

get_cd((char *)"input file.txt");

// calculate pay.

calc_cd (admin, SIZE);
calc_cd (office, SIZE);
calc_cd (field, SIZE);

// display recording employee records.

cout << "Recording Employee Records" << endl << endl;
prt_cd(admin, SIZE);
cout << endl;

return 0;
}

// define get function; A character pointer variable & points to
// file name. Reads the employee info from given file.

void get_cd(char *filename)
{

ifstream file(filename);

if (file.fail())
{
cout << "Could not find file:" << filename << endl;
return;
}

// Read data.
for (int i = 0; i < SIZE; i++)
{
struct payroll *pr = &admin[i];
// read data from file.
file >> pr->employee_id >> pr->hr_worked >> pr->payrate;
}

// Read data.
for (int i = 0; i < SIZE; i++)
{
struct payroll *pr = &office[i];
// read data from file.
file >> pr->employee_id >> pr->hr_worked >> pr->payrate;
}

// Read data.
for (int i = 0; i < SIZE; i++)
{
struct payroll *pr = &field[i];
// read data from file.
file >> pr->employee_id >> pr->hr_worked >> pr->payrate;
}

file.close(); // close file.
}

// Define calc function; uses struct payroll & employees to calculate
// the pay rate and then calculates the pay rate over 40 hours. Next it
// returns the value.

void calc_cd (struct payroll employees[], int size)
{
for (int i = 0; i < size; i++)
{
struct payroll *pr = &employees[i];
pr->total_pay = pr->hr_worked * pr->payrate;
if (pr->hr_worked > 40)
{
pr->total_pay += (pr->hr_worked - 40)/5 * pr->total_pay * 0.1;
}
}
}

// Define prt function; opens data file, formats, and writes data to file.

void prt_cd(struct payroll employees[], int size)
{
ofstream dataFile;
dataFile.open ("writeFile.txt", ios::out | ios::app);

dataFile << setw(4) << "ID" << setw(8) << "Hours" << setw(10)
<< "Pay Rate" << setw(12) << "Total Pay" << endl;

for (int i = 0; i < size; i++)
{
struct payroll *pr = &employees[i];
dataFile << setw(4) << pr->employee_id
<< setw(8) << setprecision(2) << pr->hr_worked
<< setw(10) << setprecision(2) << fixed << pr->payrate
<< setw(12) << setprecision(2) << fixed << pr->total_pay << endl;
} dataFile.close();
}



This program compiles and will read the data file, calculate data, and write calculations to the output txt file, but will not copy the first 3 lines with my name, course name, and date to the output file. I typed this on my cell phone so I apologize for any possible type-o's.

Topic archived. No new replies allowed.