- Feb 8, 2011
- 23
- 0
- 0
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.
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;
}