Help reading the file

This is the text I have:
Princeton University
NJ Princeton
41820 8014 0.0740 0.98 0.97
Harvard University
MA Cambridge
43838 19882 0.0580 0.97 0.97
Yale University
CT New Haven
45800 12109 0.0690 0.99 0.98
Columbia University
NY New York
51008 23606 0.0690 0.99 0.96
Stanford University
CA Stanford
44757 18136 0.0570 0.98 0.96
University of Chicago
IL Chicago
48253 12539 0.0880 0.99 0.93
Massachusetts Institute of Technology
MA Cambridge
45016 11301 0.0820 0.98 0.93
Duke University
NC Durham
47488 15465 0.1240 0.97 0.94
University of Pennsylvania
PA Philadelphia
47668 21358 0.1220 0.98 0.96
California Institute of Technology
CA Pasadena
43362 2181 0.1060 0.97 0.93
Dartmouth College
NH Hanover
48108 6342 0.1040 0.98 0.95
Johns Hopkins University
MD Baltimore
47060 21052 0.1710 0.97 0.93
Northwestern University
IL Evanston
47251 20997 0.1400 0.97 0.94


I want to write code that read through the whole text and show exactly same thing. However, the loop only runs for the first 4 lines and gives me 0 as below:
Princeton University
NJ Princeton
41820 8014 0.074 0.98

Harvard University
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0

Here is my code:

ifstream inFile;
inFile.open("/Users/zhongya/Desktop/Anh CIS 22A/Week7/Lab7test/Lab7test/Lab7test/universities.txt");
if(inFile.fail())
{
cout << "No Such File" << endl;
}

int i = 0;

while(i < MAX && !inFile.eof())
{
string temp;
getline(inFile, name[i]);
inFile >> state[i];
getline(inFile, city[i]);

inFile >> tuition[i] >> enrollment[i] >> retention[i] >> grad[i]; inFile >> temp;
i ++;

}
return i;

}//GetData
Last edited on
what is the value of MAX ?

while(i < MAX && !inFile.eof())
Max is 1000
After you read "temp", you still have the \n remaining in the buffer. You need to call ignore on the input stream to consume the last new line before you try to read the next school name.
Topic archived. No new replies allowed.