Mar 30, 2014 at 11:40pm UTC
so im trying to read an unspecified amount of data from a file. I tried using the .eof function to end the loop when there is no more data to view, but it is giving me an infinite loop. What is wrong with my code?
#define _USE_MATH_DEFINES
#include <iostream>
#include <iomanip>
#include <cmath>
#include <cstdlib>
#include <math.h>
#include <string>
#include <fstream>
using namespace std;
/**
* <The function reads lines of data that correspond to records of employee's data and outputs them in a specific order>
*
* @return 0 Indicating successful completion.
*/
double calcPartTimeHourly(float hours, double hrate);
double calcPartTimeSalary(float hours, double sal);
double calcFullTimeHourlyWithoutOvertime(float hours, double hrate);
double calcFullTimeHourlyWithOvertime(float hours, double hrate);
double calcFullTimeSalary(float hours, double sal);
int main()
{
string fname;
string lname;
string mname;
char minitial;
char finitial;
long employeeid;
double amount = 0;
double totalamount = 0;
double sal;
double hrate;
float totalhours = 0;
float hours;
char discard;
char employeetype;
int counter = 0;
ifstream inFile;
string fileName;
cout << "Enter the input file name: ";
cin >> fileName;
inFile.open(fileName.c_str());
while (!inFile.eof())
{
counter ++;
inFile >> discard >> employeetype >> hours;
if (employeetype == 1)
{
inFile >> hrate;
amount = calcPartTimeHourly(hours, hrate);
}
else if (employeetype == 2)
{
inFile >> sal;
amount = calcPartTimeSalary( hours, sal);
}
else if (employeetype == 3)
{
inFile >> hrate;
amount = calcFullTimeHourlyWithoutOvertime(hours, hrate);
}
else if (employeetype == 4)
{
inFile >> hrate;
amount = calcFullTimeHourlyWithOvertime(hours, hrate);
}
else if (employeetype == 5)
{
inFile >> sal;
amount = calcFullTimeSalary(hours, sal);
}
inFile >> employeeid >> fname >> mname >> lname;
finitial = fname.at(1);
minitial = mname.at(1);
totalhours += hours;
totalamount += amount;
cout << setw(12) << employeeid << setw(15) << lname << finitial << " " << minitial << " $" << amount << endl;
}
inFile.close();
cout << "Number of Employees: " << counter << endl;
cout << "Number of Hours Worked: " << totalhours << endl;
cout << "Total Payroll: $" << totalamount << endl;
return 0;
}
double calcPartTimeHourly(float hours, double hrate)
{
if (hours >= 39)
{
hours = 39;
}
double pth = hours* hrate;
return pth;
}
double calcPartTimeSalary(float hours, double sal)
{
double pts = sal/52;
return pts;
}
double calcFullTimeHourlyWithoutOvertime(float hours, double hrate)
{
if (hours >= 40)
{
hours = 40;
}
double fthwo = hours * hrate;
return fthwo;
}
double calcFullTimeHourlyWithOvertime(float hours, double hrate)
{
if (hours > 40)
{
double overhours = hours - 40;
double ftho = hrate *40;
double overhrate = hrate * 2;
ftho += (overhrate * overhours);
return ftho;
}
else
{
double ftho = hrate * hours;
return ftho;
}
}
double calcFullTimeSalary(float hours, double sal)
{
double fts = sal/52;
return fts;
}
Last edited on Mar 30, 2014 at 11:55pm UTC