a function that store data from file to an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • almo
    New Member
    • Feb 2008
    • 2

    a function that store data from file to an array

    hi guys, I have question about writing a function that will store some data from a file in an array, then when that function is call in main, I can use that array in the main() for calculation. Here is what I have for that function:

    void input(double Array[], int n)
    {
    int t;
    //double * Array;
    cout << "enter dimension of Array" << endl;
    cin >> n;
    Array = new double [n];
    ifstream inputData ("c:\\test.txt" );
    if(inputData.is _open())
    {
    //inputData.open( "c:\test.tx t" );
    for(t=0;t<n;t++ )
    {
    inputData >> Array[t];
    cout << Array[t] << endl;
    }
    inputData.close ();
    }else
    {
    cout << "unable to open file.";
    }
    }

    now if I put what isnt the void into the main code it will work fine, I am not sure how can I return this function so that when I call out this function in main, it will take the data in test.txt and store in the array I assigned in main.

    when I call out this function in main like this:
    void input(&Array, n);

    complier will have a error saying 'input' illegal use of type void and too many initializers.

    I am not sure how to correctly declare this function or call this function, would be nice if someone can help me clear things up =)

    thanks~
  • Laharl
    Recognized Expert Contributor
    • Sep 2007
    • 849

    #2
    Well...first off, do your file I/O in input() if you're going to use it. To pass in an array that needs to be changed for other use, either a) use a vector& since you're working in C++, or b) pass a pointer to the array, eg int*[] or int** due to array decay. In the Howtos section, weaknessforcats has written a very good tutorial on arrays in C/C++ that you should go read before he c/ps it in this thread.

    Comment

    • almo
      New Member
      • Feb 2008
      • 2

      #3
      thanks Laharl, I gone through the examples and stuff and clean up the code a little bit... so right now I have the file I/O in a separate function and it gets call in the the main function.

      I am declaring the input function like this:

      void input(double *Array[], int n)
      {
      int t;
      *Array = new double [n];
      ifstream inputData ("c:\\test.txt" );
      if(inputData.is _open())
      {
      for(t=0;t<n;t++ )
      {
      inputData >> *Array[t];
      cout << Array[t] << endl;
      }
      inputData.close ();
      }else
      {
      cout << "unable to open file.";
      }
      }

      but when I call it in main like this:

      void input(&Data, n);

      the compiler will return error C2182: 'input' : illegal use of type 'void'
      and C2078: too many initializers

      I am not sure what I am doing wrong in here, should I use double or something else instead of void when I declare "input" ? I tried using double instead of void and return 0 at the end of the function and that C2182 error will be gone, but I still have the too many initializer error

      any hint?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Originally posted by almo
        void input(&Data, n);
        You are calling the function. The void only appears in the function prototype and function definition.

        You should:
        Originally posted by almo
        input(&Data, n);
        Originally posted by Laharl
        In the Howtos section, weaknessforcats has written a very good tutorial on arrays in C/C++ that you should go read before he c/ps it in this thread.
        Actually, I have not written an article so I will need to c/ps it in this thread anyway. Here it is:
        Originally posted by weaknessforcats
        First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
        [code=c]
        int array[5];
        [/code]

        That is an array of 5 elements each of which is an int.

        [code=c]
        int array[];
        [/code]

        won't compile. You need to declare the number of elements.

        Second, this array:
        [code=c]
        int array[5][10];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 int.

        [code=c]
        int array[5][10][15];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


        [code=c]
        int array[][10];
        [/code]

        won't compile. You need to declare the number of elements.

        Third, the name of an array is the address of element 0
        [code=c]
        int array[5];
        [/code]

        Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

        [code=c]
        int array[5][10];
        [/code]

        Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
        [code=c]
        int array[5][10];

        int (*ptr)[10] = array;
        [/code]

        Fourth, when the number of elements is not known at compile time, you create the array dynamically:

        [code=c]
        int* array = new int[value];
        int (*ptr)[10] = new int[value][10];
        int (*ptr)[10][15] = new int[value][10][15];
        [/code]

        In each case value is the number of elements. Any other brackets only describe the elements.

        Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

        [code=c]
        int** ptr = new int[value][10]; //ERROR
        [/code]

        new returns the address of an array of 10 int and that isn't the same as an int**.

        Likewise:
        [code=c]
        int*** ptr = new int[value][10][15]; //ERROR
        [/code]

        new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

        With the above in mind this array:
        [code=cpp]
        int array[10] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Wheras this array:
        [code=cpp]
        int array[5][2] = {0,1,2,3,4,5,6, 7,8,9};
        [/code]
        has a memory layout of

        0 1 2 3 4 5 6 7 8 9

        Kinda the same, right?

        So if your disc file contains

        0 1 2 3 4 5 6 7 8 9

        Does it make a difference wheher you read into a one-dimensional array or a two-dimensional array? No.

        Therefore, when you do your read use the address of array[0][0] and read as though you have a
        one-dimensional array and the values will be in the correct locations.

        Comment

        Working...