Thread: infile inputting not working

  1. #1
    Registered User
    Join Date
    May 2010
    Posts
    2

    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

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    inFile >> firstName;
    if (inFile >> firstName)
    This reads the firstName twice

    Code:
    	if (inFile >> firstName) {  // assume rest is OK
    		dataRead = true;
    		inFile >> lastName;
    		inFile >> studentId;
    		inFile >> tuitionBalance;
    		inFile >> gpa;
    	} 
    	else {
    		dataRead = false;
    	}
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM