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;
}