Thread: problem with sending files to a class method..

  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    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;
       }
       
     }

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    infile >> arr[arrElems];
    cout << arr[arrElems] << " ";

    Presumably you meant:

    infile >> arr[i];
    cout << arr[i] << " ";


    [edit]
    while (infile.eof() == false)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    it didn't work...

    this is my output after i changed arr[arrElems] to arr[i]

    Array ar1 contains 80 values. <-- it reads correctly in the other methods because the filename was sent from int main to the parameter of the function easily..im wondering how i could KEEP the filename in the instream..it's kind of weird because i can't full the grasp the concept of SCOPE within a class and int main()

    below is when i tried to output the array

    -7.14665e+304
    4.24189e-314 1.0603e-313 0 0 0
    0 0 2.11573e-314 -8.33289e+304 0
    0 0 0 0 0
    0 -7.14665e+304 2.11573e-314 -8.98847e+307 -7.15622e+304
    -8.33437e+304 -7.15626e+304 -2.17488e+307 0 0
    8.48798e-314 4.94066e-324 -7.52996e+304 4.49863e-312 6.35972e-314
    -2.17581e+307 -8.33929e+304 -8.33289e+304 -7.13419e+304 -2.17523e+307
    0 0 0 9.46373e-319 2.03347e-309
    1.27299e-313 8.48798e-314 -NaN -NaN 0
    1.3984e-311 0 7.90505e-323 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 0 0 0 0
    0 5.09635e-309 -7.13419e+304 -3.52929e+304 2.0215e-309
    5.09619e-309 1.06037e-313 2.99373e-309 -2.17533e+307 -NaN
    1.39067e-309 0 0 0 2.11573e-314
    Last edited by utoots; 04-02-2003 at 12:05 AM.

  4. #4
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    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;
    Last edited by 7stud; 04-02-2003 at 03:11 AM.

  5. #5
    Registered User
    Join Date
    Mar 2003
    Posts
    8

    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.

  6. #6
    Registered User
    Join Date
    Apr 2003
    Posts
    2,663
    Ok, I thought that might be the case. If your teacher wants that stuff in there, then leave it.

    Also, I wasn't done with my post yet--I was still composing it. Read it again for some additional comments on why your PrintArray() function isn't working.
    Last edited by 7stud; 04-02-2003 at 01:47 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  2. Class files including other classes
    By combatdave in forum C++ Programming
    Replies: 7
    Last Post: 11-04-2006, 12:37 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. static class problem.
    By Sebastiani in forum C++ Programming
    Replies: 3
    Last Post: 10-16-2002, 03:27 PM
  5. Replies: 3
    Last Post: 12-03-2001, 01:45 PM