Reputation: 1808
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;
}
}
Upvotes: 2
Views: 169
Reputation: 1034
You keep overwriting your variables:
while (!employeeInputFile.eof())
{
employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
}
You need to save them in an intermediate like:
std::vector<EmployeeStructure> someVector;
while (!employeeInputFile.eof())
{
employeeInputFile >> employeeNumber >> employeeRate >> employeeHours;
someVector.push_back(EmpoyeeStructure(employeeNumber, employeeRate, employeeHours));
}
Then, pass that Vector around and print the information.
Upvotes: 2
Reputation: 14619
Start with omitting the while loop below:
//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;
}
And then you'll notice that you only ever get the first line of input. You should consider passing in the input stream to process_employee
.
Upvotes: 2
Reputation: 22007
The while (!employeeInputFile.eof())
means it will keep reading lines until the end of file. Each time the body is executed, it overwrites the last value read.
When process_payroll
calls process_employee
subsequent times, it reopens the stream and do the same thing again, so the same values are printed 5 times.
Upvotes: 2