I’m writing a program in C++ for school, it’s coming along, but I’m having trouble in this specific area.
I need to call a function from a loop in another function. I need to read 5 lines and put each collection of numbers read into a double value. Right now I’m just trying to make sure that I can read through the file correctly. Currently, whenever I run the program it goes through the loop and prints the info five times, but it seems to only print the numbers from the last line five times.
What in my code is making it so that my program only works with the last line of my input file?
Here’s my info:
Input File that needs to be read:
1121 15.12 40 9876 9.50 47 3333 22.00 35 2121 5.45 43 9999 10.00 25
Code that I’m working with:
double process_employee(double& employeeNumber, double& employeeRate, double& employeeHours)
{
ifstream employeeInputFile;
employeeInputFile.open("employee input file.txt");
if(employeeInputFile.fail())
{
cout << "Sorry, file could not be opened..." << endl;
system("pause");
exit(1);
}
//For some reason, this is only printing the data from the last line of my file 5 times
while (!employeeInputFile.eof())
{
employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
}
}
void process_payroll()
{
double employeeNumber = 1.0;
double employeeRate = 1.0;
double employeeHours = 1.0;
cout << "Employee Payroll" << endl << endl;
cout << "Employee Hours Rate Gross Net Fed State Soc Sec"
<< endl;
//caling process_employee 5 times because there are 5 lines in my input file
for(int i = 1; i <= 5; i++)
{
process_employee(employeeNumber, employeeRate, employeeHours);
cout << "Employee #: " << employeeNumber << " Rate: " << employeeRate << " Hours: "
<< employeeHours << endl;
}
}
You keep overwriting your variables:
You need to save them in an intermediate like:
Then, pass that Vector around and print the information.