problem with sending files to a class method..
(LOOK AT THE FUNCTION ALL THE WAY AT THE BOTTOM OF THE CODE) i'm on giving my whole code, but just the class and the method. im having a problem with the last method printArray. My professor asked us to make it ask for no parameters. im having a problem with accessing a text file sent through int main. It was first sent to function readFromFile. I was wondering how to keep the filename in my ifstream infile. I'm trying to output the filename as an array in printArray, but it's not even reading any file!, its outputting correctly at 5 numbers per line, but the number is some weird number. I know how to fix it if i could use a parameter for my printArray function, but i was instructed not to have any parameters.
Code:
#include <iostream>
#include <iomanip>
#include <fstream>
const int maxValues = 100;
ifstream infile;
ofstream outfile;
using namespace std;
// The typedef for the Array class
typedef double arrElem;
// put your Array class here
class Array
{
private:
static const int arrElems = 100; // max values that array can hold
arrElem arr[arrElems]; // the array itself
int numberUsed; // value of positions used up in array
public:
void readFromFile(char fileName[arrElems]);
void printArray(void);
int getSize();
};
char fileName[maxValues];
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;
}
int Array::getSize()
{
return numberUsed;
}
void Array::printArray() // THIS FUNCTION'S NOT WORKING CORRECTLY
{
int i;
static int perLine = 5;
for (i = 0; i < numberUsed; i++)
{
infile >> arr[arrElems];
cout << arr[arrElems] << " ";
if ((i+1) % perLine == 0)
cout << endl;
}
}
yeah i understand..but......
he instructed us to put those data members. i guess it would help us understand how classes work. he also wanted to get the point across that we can make arrElem to be double type..and i know its easier, but he just wants us to understand. i would do all that stuff you said about making things global instead of defining it within the class..that's what makes it all confusing. anyway...im just having a problem printing out the array with printArray() from the file that was sent through int main() to readFromFile(), without having to use parameters for printArray(). Again..that was specifically instructed by my professor. he sucks.