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~
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~
Comment