Member Function for calculating total

Hi,

I was wondering if someone could help me with this problem. I need to write a program that takes values from a file and assigns them to an array. It then has to calculate the total of all the values read. I have been told that I need to do this using classes and this is as far as I have got. So far I have managed to read the data from the file and assign it to each object of the class.

However, I am not able to work out how to calculate the total [adding up itsValue from each object] unless I do it outside of the class. Therefore, I was wondering if it is possible to do it within the class and if so how do I do it?

My current reasoning is if I create a private member variable called total and accessor functions, then when I use the accessor functions to set value for total, then I am going to have 10 different totals and not a running total. [there are 10 values in file]

Thanks for any help you can provide.
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
  #include <iostream>
#include <fstream>
#include <cstdlib>

using namespace std;

class FileData
{
      public:
             void set_size (int size[], int i, short count);
             int get_size () const;
             void set_total ();
             int get_total () const;
             
      private:
              int itsSize, total;
              int number_of_files;
};

const int Max = 100;//sets size of array
int size [Max];//array used to store data from file
FileData FileSize [Max];//creating object

//member function setting value of itsSize using the array which holds the values read from file
void FileData::set_size (int size[], int i, short count)
{
     itsSize = size[i];
     number_of_files = count;
}//end of member function

//member function returns value of itsSize
int FileData::get_size () const
{
    return itsSize;
}//end of member function

//FUNCTIONS
void read_data (short &count, int size []);
//end of functions

int main ()//start of main
{
    short count = 0;//used for controlling element of array and storing value for number of files read
    int i = 0;
    
    read_data (count, size);//function call
    for (i = 0; i < count; i++)
    {
        FileSize [i].set_size(size, i, count);
    }
    for (int j = 0; j < count; j++)
    {
        cout << "Size of file " << j+1 << ": " << FileSize[j].get_size() << endl;
    }
    
    //cout << "Out of loop - Count is: " << count << endl;
    system ("PAUSE");//pauses console for reading display
    return 0;
}//end of main

//function reads data from file and assigns to array. Also calculates number of files read.
void read_data (short &count, int size [])
{
    ifstream myData("i:\\filesize.txt",ios::in); //opening the file for reading.
    if (myData.is_open()) //tests if the file is open
    {
        while (! myData.eof() ) //while the end of file is NOT reached
        {
              myData >> size[count];//reading data from file and assigning to each element of the array
              //cout << "size of file " << count << " is: " << size[count] << endl;
              count++;//counting how many files have been read
        }
        //cout << "Count is: " << count << endl;
        myData.close(); //closing the file
    }
    else cout << "Unable to open file"; //if the file is not open output 
}//end of function 
You have an array of FileData objects. The point of the class is that you can store the data into the class and access/manipulate the data with methods.

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
#include <string>
#include <fstream>
#include <iostream>

class FileData
{
private:
  int m_data[100];
  int m_size;
public:
  FileData() : size(0) // initialize size to 0
  {
  } 
  
  int size() 
  { 
    return m_size; 
  }
  
  void read( string filename )
  {
    ifstream fin( filename , ios::in );
    if (fin.is_open())
    {
      while (! fin.eof() )
        fin >> m_data[ m_size++ ];
      
      fin.close();
    }
    else
    {
      cout << "Unable to open " << filename;
    }
  }
  
  int total()
  {
    int output = 0;
    for (int i = 0; i < m_size; i++)
      output += m_data[i];
    return output;
  }
};

int main()
{
  FileData fd;
  fd.read("i:\\filesize.txt");
  cout << "size: "  << fd.size()  << endl
       << "total: " << fd.total() << endl;
}
Last edited on
Hi Stewbond,

Not sure if you noticed but I am relatively new to classes and am still trying to get my head around them.

I have had a quick look through your code and can follow most of it. Going to have a mess around and see if I can work it all out.

If there is something I don't get I will be sure to ask for more help. However, I will post back even if it is just to say that I don't need more help.

Thanks very much for the help so far, really appreciate it.
Hi Stewbond,

Just wanted to thank you again for your help, it was really useful for me. I changed some of the variable names, changed the layout and a couple of minor things, plus added in a couple of new functions and it is working great.

Really appreciate it mate :)

Topic archived. No new replies allowed.