Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this function. I\'m using a struct (I have to use one) and I can\'t get a

ID: 3620884 • Letter: I

Question

I have this function. I'm using a struct (I have to use one) and I can't get anything to be read from the infile.

struct EmpType
{
string employee;
string employeeName;
double sal;
double tax;
};

void ReadFile(const int MAXI, int& i, double& salSum, EmpType emp[])
{
ifstream myInFile; // file from which to read
string inFileName; //name of file to ask user
cout << "Enter file name to open file including extension" << endl; //prompt user
cin >> inFileName; //reads in file name
myInFile.open(inFileName.c_str());
while (!myInFile) // to check file opened correctly
{
cout << "Enter file name to open file" << endl; //prompt user again
cin >> inFileName;
myInFile.open(inFileName.c_str());
}
while (myInFile.eof()) //search for end of file
{
getline(myInFile, emp[i].employeeName);
myInFile >> emp[i].sal >> emp[i].employee; // Saves the double in array amd saves string of employee id in array
salSum = salSum + emp[i].sal; // computes sum of salaries as they are read.
i++;
if (i > MAXI) // make sure to not take up more space than allowed
break;
}
myInFile.close();//close in file
}

My inFile looks like this


Rodney McKay
12345.23 A2343
Evian C. Fraizer
1234233.50 A2534
Harry Potter
3324.09 A2472
Crazy Eyes
345532.03 A2374
Ninja Fighter
106800.00 A5432

basically a line (getline is used to get the) name, then the next like has the salary and then the employee id.

Explanation / Answer

Hope it's this simple. while (myInFile.eof()) should be while (!myInFile.eof())