C++ Help with homework! The topic is Sequential Files.

Hi! I am a beginner to C++ programming and I am completed stumped on one of my homework labs. I will post the homework instructions and what I have done so far. I appreciate any tips or guidance!

1) Use the notepad program to create a payroll file for 20 records as follow: Employee’s First name, Last name, Social Security Number, Hours worked and Hourly rate.
2) Write a C++ program to read this file into your program and do the proper calculation including overtime if any. Overtime is times and half of the pay rate. Deduct 2% for Social Security, 5% retirement plan, 3% Federal tax (total of 10% deduction). Create a report as follow: Last four digit of SSN, Employee’s first initial and last name, Hours worked, Hourly rate, Gross pay, Total deductions, and Net pay. You may have to use “substr” for SSN and Employees first initial.
Make sure you have number of records and thank you note as a summary at the bottom.

What I have done so far:

#include <iostream>
#include <fstream>
using namespace std;

int main()
system("color 1f");

ifstream infile;

string fname = "", lname = "", full = "";
full = fname + " " + lname;
int ssn = 0, hoursW = 0, rate = 0, grossPay = 0, deductions = 0, netPay = 0, cnt = 0;

infile >> fname >> lname >> ssn >> hoursW >> rate;
cout << "\tFName\tLName\tSSN\tHours\tRate" << endl;
cout << "\t----\t---\t-----\t----" << endl;
while (infile.eof() == false)
cout << "\t" << fname << "\t" << lname << "\t" << ssn << "\t" << hoursW << "\t" << rate << endl;
infile >> fname >> lname >> ssn >> hoursW >> rate;
cout << "\n\tNumber of record: " << cnt << endl;
cout << "\n\t\tT H A N K Y O U\n" << endl;
return 0;
When posting code, please use code tags so that the code is readable

// code code here

The loop to read data from the file isn't right. .eof isn't set at the end of a read but when a read is attempted. Hence the while loop should have the file extraction as the condition - not .eof().

Once you have that sorted and you have tested that you are reading the file data correctly, what other part of the assignment doesn't you understand?
Topic archived. No new replies allowed.