Hey guys. I am new programmer and can't figure out this while loop. I am getting input from a .dat file that looks like this:
Bryan 100000.75
Elise 826
James 12000.50
Johnny 900000.75
Roger 77000.50
Belle 59576
Bill 10.00
Larry 96000.25
Eric 100
The first row is names, the second row is salaries. My program needs to use a loop to gather the info from this data file and determine who has the max salary, who has the min salary and what is the average of all the salaries in the data file. Here is my loop:
in_data.open("emps.dat");
decimals(cout,2);
while(!in_data.eof())
{
in_data >> temp;
for(i = i; i < 2; i++)
{
max.set_salary(temp.salary());
max.set_name(temp.name());
min.set_salary(temp.salary());
min.set_name(temp.name());
}
if(temp.salary() > max.salary())
{
max.set_salary(temp.salary());
max.set_name(temp.name());
total_sal = total_sal + max.salary();
counter++;
}
else if (temp.salary() < min.salary())
{
min.set_salary(temp.salary());
min.set_name(temp.name());
total_sal = total_sal + min.salary();
counter++;
}
else
{
total_sal = total_sal + temp.salary();
counter++;
}
}
So my problem is, it keeps adding the last member in the data file twice to total_sal. What can I do to stop this from happening?
So in this data files case, the total would be $100 too much.
(PS the program must be able to handle any data file of any size.)



LinkBack URL
About LinkBacks



