I am absolutely HORRIBLE at passing functions, and even more so with passing arrays. I'm trying to pass my "grades" array to the other functions, but I know I have to do it by reference since I can't return it. Can someone help shed some light?

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

int readFile(int, int &count, int (&grades [10]));
int writeFile();
int averageGrades(int, int);

/**********************************************************************
* Reads an external file of grades and then returns those values to
* other functions in the program.
*********************************************************************/
int readFile(int sum, int &count, int (&grades [10]))
{
   //Declare variable
   char sourceFile[16];
   ifstream inStream;

   //Asking for user input
   cout << "Source file: ";
   cin >> sourceFile;

   //Open file
   inStream.open(sourceFile);
   if (inStream.fail())
   {
      cout << "Input file opening failed.\n";
      exit(1);
   }

   //Read from file and place in array
   for (int i = 0; i < 10 && inStream >> grades[i]; i++)
   {
      if (grades[i] == -1)
      {
         count++;
      }

      if (grades[i] != -1)
      {
         sum += grades[i];
      }
   }
   return sum;
}

/**********************************************************************
 * Reads an external file of grades and then returns those values to
 * other functions in the program.
 *********************************************************************/
int writeFile()
{
   ofstream outStream;
   char destinationFile[16];

   //Asking for user input
   cout << "Destination file: ";
   cin >> destinationFile;

   //Open file
   outStream.open(destinationFile);
   if (outStream.fail())
   {
      cout << "Output file opening failed.\n";
      exit(1);
   }

   outStream << grades[i];

   return 0;
}


/**********************************************************************
* Finds the average of the ten grades from the previous function.
***********************************************************************/
int averageGrades(int sum, int count, int average)
{
   //The magic formula to find the average
   if (count == 10)
   {
      cout << "Average Grade: ---%" << endl;
   }

   else
   {
      average = (sum / (10 - count));
      cout << "Average Grade: " << average << "%" << endl;
   }

   return average;
}


/**********************************************************************
* Basically a delegator. Calls other functions to do its dirty work.
***********************************************************************/
int main()
{
   //Declaring Variables
   int average = 0;
   int sum = 0;
   int count = 0;
   int grades[10];

   //Calling other functions
   sum = readFile(sum, count);
   average = averageGrades(sum, count, average);

   return 0;
}

Here's a simple way to pass an array.

#include <iostream>

void myfunc(int * x)
{
	for (int i = 0; i < 7; ++i)
		std::cout << x[i] << std::endl;
}

int main()
{
	int mya[] = {1, 2, 3, 4, 5, 6, 7};

	myfunc(mya);
	return 0;
}

To expand on what Gerard pointed out is when you pass an array to a function it actually only passes a pointer to the first element. Since you are working with pointers any change to a value in the array in a function is reflected in main.

void foo(int[]);
// or
void foo(int[SOME_NUMBER]);

// is the same as
void foo(int *);

I'm going to disagree with both gerard4143 and NathanOliver here. You can pass an array by reference as follows:

1) Change line 14 to the following:

int readFile(int sum, int &count, int (&grades) [10])

In case you're wondering what I changed, I moved one parenthesis.

2) Change line 107 to the following:

sum = readfile(sum, count, grades);

There may be other errors, but with a little luck, these suggestions will get you started in the right direction.

But there is no need for him to pass it by reference. I'm not saying you cant but asking why you would want to.

But there is no need for him to pass it by reference. I'm not saying you cant but asking why you would want to.

I have no idea :) Perhaps part of the original problem was to learn something about references.

I was just answering the question that was asked.

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.