Hi,
You need to try and understand these lines:
Code:
class Array
{
private:
static const int arrElems = 100; // max values that array can hold
arrElem arr[arrElems];
Why are you making the variable arrElems static, and why are you making it a data member of your class? Also, why are you using the typedef:
typedef double arrElem;
It just makes your code confusing, and "arrElem" is harder to type than "double" anyway. If you don't have a good reason for doing all the above, change your code to:
Code:
class Array
{
private:
double arr[100];
Now, 100 is the size of the array, so as was pointed out in the previous post, you don't cycle through an array like this:
Code:
for(int i=0; i<numberUsed; i++)
{
infile >> arr[100];
cout << arr[100];
}
and that is exactly what you are doing with this code in your PrintArray() function:
Code:
for(int i=0; i<numberUsed; i++)
{
infile >> arr[arrElems];
cout << arr[arrElems];
}
because you defined arrElems to be equal to 100. So, the result is you repeatedly read in data and overwrite index 100--and guess what? There's no index 100 for your array. Index 100 is out of bounds, and C++ lets you go out of bounds without the compiler flagging it as an error, so you get all kinds of bad things happening when you run your program. When you declare an array to be size 100, the index values run from 0 to 99.
Ok, now it's time to talk about the scope of a class. In your PrintArray() function how do you print out the array if the array isn't passed to the function as an argument? Since the array exists somewhere outside the function, it's not possible right? Wrong. You have to remember, the PrintArray() function is a class function, so when you invoke the function, you are invoking it on a class object like this:
myArrayObject.PrintArray()
and a class function has access to all the data members of the object it's called on. So, now we're getting somewhere. What data members are part of the object and therefore accessible to the function? Looking down the list of the private data members, I see one that is an array. So, you can reference that variable in your PrintArray() function by using its name, arr:
cout arr[i];
So, when you're defining class functions remember: you can use the names of any data member in the class whether public or private in the body of your functions. Here is a simple example:
Code:
#include <iostream>
using namespace std;
class Array
{
private:
double numbers[3];
public:
Array()
{
for(int i=0; i<3; i++)
numbers[i]=i;//referencing private member
}
void PrintArray()
{
for(int i=0;i<3; i++)
cout<<numbers[i]<<endl;//referencing private member
}
};
int main()
{
Array a;
a.PrintArray();
return 0;
}
Here is another misuse of static:
Code:
void Array::readFromFile(char fileName[arrElems])
{
static int filesize = 0;
infile.open(fileName);
if (infile.fail())
{
cout << "Opening file " << fileName << " failed. Goodbye." << endl;
exit(1);
}
infile >> ws; // to make sure it doesn't read last value twice
while (infile.eof() == false)
{
filesize++;
infile >> arr[arrElems];
infile >> ws;
if (filesize > 100)
{
cout << "There are more values in the file than there are positions"
<< " in the array." << endl;
}
}
numberUsed = filesize;
}
The variable filesize never changes, so it certainly doesn't need to be static, and I question why you need that variable at all. If you are using it to avoid "magic numbers" being hard coded into your function, then just declare it as a local variable:
const int filesize=0;