Files and Class Array

I have created a file with information pertaining to the information in my class.
1) How do I take in the file?
2) How do I create an array of an class?
3) Will this all connect together in anyway?

Here is my masterpiece.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifstream ifs("NameStuds.txt");
ofstream ofs("NameStudsout.txt");

    if((ifs == 0) || (ofs == 0)) {
        cout << "Not found, ending program!" << endl;
        return 0;
    }

     for (int i = 0; i < 5; i++) {
            ifs >> students[i];
            cout << students[i];
    }

    for (int i = 0; i < 5; i++) {
            ofs << students[i] << endl;
    }


Thank You.
Last edited on
Without seeing the rest of your files or the code it's hard to say. Have you overloaded the >> and << operators to work with your class? If not, then lines 10, 11 and 15 probably won't work the way you intended them to.

To create an array of a class you simply use the same notation you do with built in types. The format is typename variablename[array size];

So say I had a class called Furniture and I wanted to declare an array of 10 of them, all called desks. I could declare this as:

Furniture deskArray[10];

Each element of deskArray is now a type Furniture object. To access them you simply use

1
2
deskArray[i].somedatamember;
deskArray[i].somefunctionmember;


where [i] is the element of the object you want to access.
Last edited on
Topic archived. No new replies allowed.