How to transfer an array to a class array?

We had a lab in my class today where we had to send file data (20 elements) into a class array (not an array of a class, but an array IN the class). The following is the way in which I did that:

Header File:
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
#ifndef LABFOUR_H
#define LABFOUR_H
#include <iostream>
using namespace std;

//Class is defined
class SearchCount
{
  private:
    //Declares the number of elements for the class array
    static const int SIZE=20;
    int number[SIZE];
    int counter;
  public:
    //Constructor
    SearchCount()
    {
        //Initializes the array elements to 0
        for(int i=0;i<SIZE;i++)
            number[i]=0;

        counter=0;
    }
    
    //Mutator Functions
    void setNumber(int n)
    { 
        number[counter]=n;
        counter++;
    }
    void setCount(int co)
    { counter=co; }
    
    //Accessor Functions
    int getNumber(int c)
    { return number[c]; }
    int getCount()
    { return counter; }
    
};
#endif 


C++ File
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
/*This program allows the user to search for a number using either linear
or binary means (Bubble and Selection sorting for binary)*/
#include <iostream>
#include <fstream>
#include "LabFour.h"
using namespace std;

//Simple functions to read file and display menu
void readFile(SearchCount[],int);

//**************************************************
//Main function
int main()
{
    //Class object array is declared
    const int NUM=3;
    SearchCount order[NUM];
    int option,
        value,
        //Holds values received from class functions
        tempInt;

    //Reads file
    readFile(order,NUM);

    return 0;
}

//**************************************************
/*Reads the file and saves the values equally among the
object array*/
void readFile(SearchCount order[], int NUM)
{
    int tempInt;

    ifstream readFile;
    readFile.open("numbers.txt");
    if(!readFile)
    {
        cout<<"Error opening file."<<endl;
        cout<<"Press enter to exit the program."<<endl;
        cin.get();
        exit(0);
    }

    while(readFile>>tempInt)
    {
        //Assigns tempInt to every array element before reading the next number
        //Loops depend on the size of the array
        for(int i=0;i<NUM;i++)
        {
            order[i].setNumber(tempInt,i);
        }
    }

    //Closes file
    readFile.close();
}


Mind any things that seem out of place, I cut out what it is that I wanted to change so this is not the whole program.

The thing is, when I asked the professor about it he said it was fine. But when I asked him if I could have done it better, he said it would have been easier to just read and transfer the whole array from the main file. I am not sure what he meant by that and I didn't have time to ask him at the end of the class. Can anyone enlighten me on it, or perhaps suggest a better way of doing this? I know my own way is faulty because if they decided to initialize some numbers, it would get screwed.

Thank in advance and very much appreciated.
Last edited on
Topic archived. No new replies allowed.