Thread: Help with passing a file into multiple arrays

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    13

    Help with passing a file into multiple arrays

    Anyone who could give me advice on this, I would greatly appreciate it. I've spent most of my day searching for something that made sense and found a few things but I'm totally stuck at this point. I have to read a .dat file that I have into 3 separate arrays. Here is what I have so far...

    students.dat file

    LENNON 1 4.0
    MACARTNEY 2 3.7
    HARRISON 3 2.95
    STARR 3 3.97
    HOWARD 1 3.2
    FINE 3 2.11
    BESSER 2 4.0
    DERITA 1 3.9


    Each line represents one student. All columns are separated by one space. Assume there are up to 100 students. Will never be more than 100, but possibly up to 100. Program has to read students.dat file into 3 arrays, one array for each column.

    The name has to be a string type
    The second column is an int type
    The third is a double

    I'm totally lost about how to pass these into the arrays. Here is my code so far. It has to be 3 arrays, can't use anything else. Thanks in advance for any help.

    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    #include <iomanip>
    
    using namespace std;
    
    //Set constant for length of array
    const int arrayLength = 100;
    
    int main( ) {
    
    	//Set file variable and open file
    	ifstream inFile;
    	inFile.open("students.dat");
    
    	//Set error if file name is not correct
    	if(inFile.fail()) {
    		cout << "Error: Can't open file. Recheck your filenames" << endl;
    		return 0;
    	}
    	
    	//Declare arrays for file input	
    	string lastName[arrayLength];
    	int classID[arrayLength];
    	double gpa[arrayLength];
    
    	while (! inFile.eof()) {
    
    		}
    			
    	cout << "Welcome to Miss Winfred Fowl's GPA Report" << endl;
    	cout << endl;
    	cout << "All Students Report" << endl;
    	
    
    	//Set while loop with break to run loop until DONE is entered
    	while(true) {
    		
    		string studentName;
    	
    		// Have the user input a name or DONE to quit the loop
                    cout << endl;
    		cout << "Please enter a student name: ";
    		cin >> studentName;
    	
    		if(studentName == "DONE") {
    			break;
    		}
    	}	
    
    
    	
    	return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Your empty while loop needs something like

    inFile >> lastName[index];

    And you increment index each time around the loop.


    Also, there is a FAQ entry on why
    while (! inFile.eof())
    is a bad loop construct.
    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. Passing data from file to arrays
    By Chris5 in forum C Programming
    Replies: 16
    Last Post: 12-06-2010, 11:13 PM
  2. Passing pointers to arrays of char arrays
    By bobthebullet990 in forum C Programming
    Replies: 5
    Last Post: 03-31-2006, 05:31 AM
  3. Replies: 2
    Last Post: 02-23-2004, 06:34 AM
  4. passing arrays and passing pointers
    By Leeman_s in forum C++ Programming
    Replies: 3
    Last Post: 04-13-2002, 12:35 PM
  5. Parallel Arrays with Multiple Arrays
    By Billye Scott in forum C++ Programming
    Replies: 0
    Last Post: 03-02-2002, 11:14 PM