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]
#include <iostream>
#include <fstream>
#include <cstdlib>
usingnamespace 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;
};
constint 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.
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.
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.