Reading file to class variables

Hi I've been trying to read elements of a file to my class variables but it's giving me a read error. Would be great if someone could look through my code, I mainly need help with adding all 4 students into student class objects. Perhaps an array of student objects.

My file that I am reading from is ProgramSixData.txt:

George
75,85,95,100,44
Peter
100,100,100,100,100
Frank
44,55,66,77,88
Alfred
99,88,77,66,55

My header file with my Student class object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
const int SIZE_OF = 5;

class Student
{
public:
    Student();
    Student(const Student &);
    Student(string, int, int, int, int, int);
    friend std::istream& operator >> (std::istream& in, Student& S);
    void display();
private:
    string lastName;
    int grades[SIZE_OF];
};


Then my function definitions for my class object:

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
54
55
56
57
58
59
60
61
62
Student::Student()
{
    int i;
    string lastName = "default";
    for (i = 0; i < 5; i++)
    {
        grades[i] = 0;
    }

}

Student::Student(const Student & S)
{
    int i;
    lastName = S.lastName;
    for (i = 0; i < 5; i++)
    {
        grades[i] = S.grades[i];
    }
}

Student::Student(string S, int a, int b, int c, int d, int e)
{
    lastName = S;
    grades[0] = a;
    grades[1] = b;
    grades[2] = c;
    grades[3] = d;
    grades[4] = e;
}

std::istream& operator >> (std::istream& in, Student& S)
{
    char dummy;
    in >> S.lastName >> S.grades[0]
        >> dummy >> S.grades[1]
        >> dummy >> S.grades[2]
        >> dummy >> S.grades[3]
        >> dummy >> S.grades[4];
    return in;

}

void Student::display()
{
    int i;
    int sum = 0;
    double average;
    cout << "Last Name: " << lastName << endl;
    cout << "Grades: " << endl;
    for (i = 0; i < 5; i++)
    {
        cout << grades[i] << endl;
    }
    for (i = 0; i < 5; i++)
    {
        sum = sum + grades[i];
    }
    average = sum / 5;
    cout << "Average: " << average;

}


Finally my main cpp file, I am encountering the "read failed" error from my loop when it encounters File.Fail().

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
void main()
{
    fstream     File;
    string      FileName = "ProgramSixData.txt";
    bool        FoundFile;
    string      Line;
    Student     testStudent;

    do {
        File.open(FileName, ios_base::in | ios_base::out);
        FoundFile = File.is_open();
        if (!FoundFile)
        {
            cout << "Could not open file named " << FileName << endl;
            File.open(FileName, ios_base::out); // try to create it
            FoundFile = File.is_open();
            if (!FoundFile)
            {
                cout << "Could not create file named " << FileName << endl;
                exit(0);
            }
            else;
        }
        else;
    } while (!FoundFile);
    do {
        File >> testStudent;
        if (File.fail())
        {
            cout << "Read Failed" << endl;
            cout << "Bye" << endl;
            exit(0);
        }
        else;
        testStudent.display();
    } while (!File.eof());
    cout << "Bye" << endl;
    File.close();
}


How do I read each of the names and associated grades from the text file into the class variables? How do I make the file stop reading after it's done with one student's name and grades and create the next class object student2 for the second student's names and grades?
Last edited on
Hi,
1
2
3
File.open(FileName, ios_base::in | ios_base:: out);

File.open(FileName, ios_base:: out); // try to create it 

Just one is enough.
Does that help? :)
Topic archived. No new replies allowed.