I'm having a lot of trouble figuring out how to pass values back and forth between functions.
In the program I'm working on now, I'm supposed to declare an integer array (n[15000]) and an integer (count) in main, then pass those to a function which opens a txt file and then starts a loop. The loop takes the values in the txt file and stores them in n[], while count keeps track of how many times the loop runs. At the end the function should pass n[] and count back to main, then main prints the results. That's where I'm having trouble.
Here's a sample of my code:
Code:
#include <iostream>
#include <fstream>
using namespace std;
int ReadFile(int n[], int count);
int main()
{
int n[15000];
int count = 0;
ReadFile(n, count);
cout << "There are " << count << " numbers in the file.\n";
cout << "The first number is " << n[0] << endl;
return 0;
}
int ReadFile(int n[], int count)
{
ifstream input;
input.open("Numbers.txt");
if(!input)
{
cout << "Cannot open file" << endl;
return 0;
}
while (!input.eof())
{
input >> n[count];
++count;
}
input.close();
return n, count;
}
For some reason n[] makes it through and displays the correct number, but count always shows up as 0. Can anyone explain what I'm doing wrong?