Trailer values with class arrays
Apr 18, 2013 at 2:54pm UTC
I'm stumped. For some reason when I read the file I only get back weird numbers. The info in the file is the first five lines above the code below. I've tried this on vim and xcode but I don't know what's wrong. Sorry if I don't have more info.
Thanks
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
// Bishop Peter 3.9 30
// Bishop Wlater 3.9 29
// Bell William 3.9 31
// Dunham Olivia 3.7 29
// ****
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
using namespace std;
ifstream infile;
class Student {
public :
string firstName;
string lastName;
double gpa;
int totalCredits;
};
int main() {
infile.open("file.txt" );
if (!infile){
cerr << "Error" << endl;
exit(1);
}
int count = 0, maxElements = 5;
Student student[5];
string lastName;
infile >> lastName;
while (lastName != "****" ){
if (count >= maxElements) {
cerr << "Array is too small; please resize, recompile, and re-execute" << endl;
exit(1);
}
student[count].lastName = lastName;
infile >> student[count].firstName;
infile >> student[count].gpa;
infile >> student[count].totalCredits;
count++;
cout << student[count].lastName << " " << student[count].firstName << " " ;
cout << student[count].gpa << " " << student[count].totalCredits << endl;
infile >> lastName;
}
infile.close();
return 0;
}
Apr 19, 2013 at 5:56pm UTC
I figured it out. I just had to move the count to the end.
Topic archived. No new replies allowed.