Hello. Im trying have a program read values from an outside file and put those values into an array using a second function. I'm not sure the right method to call it. Or if i even set it up the correct way.
In addition to what Peter87 has said in your function when you open a file for input you should check and make sure the file is open and that you have a usable stream. Otherwise you could be using a bad stream in the for loop and nothing will be put in your array.
1 2 3 4 5 6 7 8
file.open(getFilename);
if (!file)
{
std::cout << "\n Your Error massage here" << std::endl;
std::this_thread::sleep_for(std::chrono::seconds(5)); // Requires header files "chrono" and "thread"
exit(1); // <--- No point in continuing.
}
Also notice the first line. From C++11 on a "std::string" will work. There is no need to convert it to a C string.
FYI in the read function you define int numbers[NUMBERS];. This is fine except that it is a local variable that you read your file into and when the function ends this array is destroyed and you no longer have any access to it.
Backing up to main cin >> getFilename; will work fine if the file name is "FileName", but if it is "File Name" the variable getFilename will only contain "File" as the formatted input will stop at the first white space it finds. a better choice here is std::getline(std::cin, getFilename);. This will get whatever you type including the new line character at the end which is eventually discarded.
Lastly you have an input file that you are using. It is helpful if you post the input file or at least a sample of what you are using because those of us on this end may see something that you do not.
After working with your program there are a couple of things I found:
First you are using a "std::string", but yo did not include the header file <string>.
Line 7 should be above line 5 because the compiler will not see line 7 and you may need it in line 5.
Even though you do not use it or need it you are trying to pass an array to a function by reference, int& arrayS, This is not the correct way. It should look like this int (&array)S[NUMBERS]. When passing by reference you will need to give the dimension(s) a proper size for it to work correctly. And both dimensions are needed for a 2D array.
I did change some parts of the program mostly for testing to make it easier.