By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,620 Members | 1,512 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,620 IT Pros & Developers. It's quick & easy.

a function that store data from file to an array

P: 2
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.txt" );
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~
Feb 13 '08 #1
Share this Question
Share on Google+
3 Replies


Expert 100+
P: 849
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.
Feb 13 '08 #2

P: 2
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?
Feb 13 '08 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
void input(&Data, n);
You are calling the function. The void only appears in the function prototype and function definition.

You should:
input(&Data, n);
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:
First, there are only one-dimensional arrays in C or C++. The number of elements in put between brackets:
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
That is an array of 5 elements each of which is an int.

Expand|Select|Wrap|Line Numbers
  1. int array[];
  2.  
won't compile. You need to declare the number of elements.

Second, this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
is still an array of 5 elements. Each element is an array of 10 int.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10][15];
  2.  
is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


Expand|Select|Wrap|Line Numbers
  1. int array[][10];
  2.  
won't compile. You need to declare the number of elements.

Third, the name of an array is the address of element 0
Expand|Select|Wrap|Line Numbers
  1. int array[5];
  2.  
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*.

Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. int array[5][10];
  2.  
  3. int (*ptr)[10] = array;
  4.  
Fourth, when the number of elements is not known at compile time, you create the array dynamically:

Expand|Select|Wrap|Line Numbers
  1. int* array = new int[value];
  2. int (*ptr)[10] = new int[value][10];
  3. int (*ptr)[10][15] = new int[value][10][15];
  4.  
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:

Expand|Select|Wrap|Line Numbers
  1. int** ptr = new int[value][10];    //ERROR
  2.  
new returns the address of an array of 10 int and that isn't the same as an int**.

Likewise:
Expand|Select|Wrap|Line Numbers
  1. int*** ptr = new int[value][10][15];    //ERROR
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. int array[10] = {0,1,2,3,4,5,6,7,8,9};
  2.  
has a memory layout of

0 1 2 3 4 5 6 7 8 9

Wheras this array:
Expand|Select|Wrap|Line Numbers
  1. int array[5][2] = {0,1,2,3,4,5,6,7,8,9};
  2.  
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.
Feb 13 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.