infile inputting not working
I am using several cpp and .h files and currently trying to build / working on a function that will open a file then through another function read into an array class the data member types from the class and for some reason they are not importing the data from the file to the array
the code to open the file is as follows:
Code:
int readData(Student students[])
{
int numStudents = 0;
ifstream inFile;
int i = 0;
inFile.open("students.txt");
if (inFile.fail())
{
cout << "Error opening input file! Terminating application!" << endl;
exit(1);
}
while (students[i].load(inFile))
{
i++;
numStudents++;
}
inFile.close();
return numStudents;
}
the second part that is Student::load goes as follows:
Code:
bool Student::load(ifstream &InFile)
{
bool dataRead = false;
inFile >> firstName;
if (inFile >> firstName)
dataRead = true;
else
dataRead = false;
inFile >> lastName;
inFile >> studentId;
inFile >> tuitionBalance;
inFile >> gpa;
return dataRead;
}
Student is built as a class as follows.
Code:
Student::Student()
{
setFirstName("");
setLastName("");
setStudentId ("");
setTuitionBalance (0.0);
setGpa (0.0);
}
Student::Student(string pFirstName, string pLastName, string pStudentId,
double pTuitionBalance, double pGpa)
{
setFirstName(pFirstName);
setLastName(pLastName);
setStudentId (pStudentId);
setTuitionBalance(pTuitionBalance);
setGpa (pGpa);
}
void Student::setFirstName(string pFirstName)
{
firstName = pFirstName;
}
string Student::getFirstName()
{
return firstName;
}
void Student::setLastName(string pLastName)
{
lastName = pLastName;
}
string Student::getLastName()
{
return lastName;
}
void Student::setStudentId(string pStudentId)
{
studentId = pStudentId;
}
string Student::getStudentId()
{
return studentId;
}
void Student::setTuitionBalance(double pTuitionBalance)
{
if (pTuitionBalance <= 0)
tuitionBalance = 0;
else
tuitionBalance = pTuitionBalance;
}
double Student::getTuitionBalance()
{
return tuitionBalance;
}
void Student::setGpa(double pGpa)
{
gpa = pGpa;
}
double Student::getGpa()
{
return gpa;
}
// payTuition will take the TuitionBalance and subtract a payment ammount from
// it to pay the tuition bill.
void Student::payTuition(double pPayment)
{
tuitionBalance -= payment;
}
// displayData will take the data and display it in a row with various widths
// being set for each display field, it will also provide 2 points of precision
// and display the decimal.
void Student::displayData()
{
cout << left << setw(15) << firstName;
cout << setw(15) << lastName;
cout << setw(10) << studentId;
cout << setw(10) << showpoint << setprecision(2) << "$" << tuitionBalance;
cout << setw(10) << showpoint << setprecision(2) << gpa; // may need to throw an endl after this.
}
bool Student::load(ifstream &InFile)
{
bool dataRead = false;
inFile >> firstName;
if (inFile >> firstName)
dataRead = true;
else
dataRead = false;
inFile >> lastName;
inFile >> studentId;
inFile >> tuitionBalance;
inFile >> gpa;
return dataRead;
}
// Store will take data from being read in and store it into an output file
// using various widths to align the data uniformly.
void Student::store(ofstream &OutFile)
{
outFile << left << setw(15) << firstName;
outFile << setw(15) << lastName;
outFile << setw(10) << studentId;
outFile << setw(10) << showpoint << setprecision(2) << "$" << tuitionBalance;
outFile << setw(10) << showpoint << setprecision(2) << gpa; // may need to throw an endl after this.
}
any help would be nice as to why the infile will not show any information or import it.
inside of students.txt i have set up the following line:
ralph johnson 19387 1456.50 2.40