I need to have my program use a function to read in numbers from a file to an array, and pass the array back to my main program to be outputted/used by another function; however, I am getting an error(cannot convert from 'int' to 'int [100]'): Here is the relevant code I have so far:

#include<iostream>
#include<fstream>
using namespace std;

int readFile(int arg[]);

void main()
{
int numarray[100];

[b]numarray = readFile(numarray[])[/b]

}

int readFile(int nums[])
{
	int x = 1;
	fstream numfile("numbers.txt"); 
	while(! numfile.eof() )
	{
		numfile >> nums[x];
		x++;
	}
 
	return nums;
}

Note: Bolded line is where the compiler says the error is.

I initially tried to create the array inside the function, but read that passing it back wouldn't work, as I was passing back a reference to a local memory location only valid inside the function. So I tried creating the array in main, passing to the function, reading in, and returning it, but it still gives me the "cannot convert" error. How can I fix it so it doesn't give me this error?

I apologize if this has been answered before, but all the threads I read have confusing examples that don't seem to work in my application. Thanks in advance!

>numarray = readFile(numarray[])
Remove the [] and it'll pass the address of the starting element of the array. You also don't need to bother returning the array, since you've passed the memory address of it to the function. Therefore, any changes made are made on the original object. The line above should be changed to: readFile ( numarray ); Keep in mind that it's bad practice to assume that the file won't be larger than the bounds of your array. Ideally, readFile() should accept the size of the array as a parameter, and will refuse to read farther than the bounds of the array.

Ahh, I see. Works perfectly when I don't return the function(and leave out the [] in that spot). Thank you!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.