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>
usingnamespace 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;
}
Arrays are by default passed by reference; you know that, right? Also you don't need to declare the size of the array your passing if and only if it's one-dimensional. :P
Aside from that, is there anything else you needed help with? (Are you sure you have your functions right at lines 108 and 109?)
What do you mean they're passed by default? Meaning, I don't have to pass them at all? How does that work then? Because when I to use "grades" in any other function, it gives me errors that say, "blah blah blah not declared in this scope."
I'm confused... This program seems a little over my head.