so I am trying to take in a sample file, read it, find the average, and then display it. I think that I have most of what I am looking for, however I can't seem to display the average. could someone point out to me what I am doing wrong? thank you.
#include <iostream>
#include <fstream>
using namespace std;
void getFileName(char filename[])
{
cout << "Please specify the file name: ";
cin >> filename;
}
int readFile(char filename[], float grades[])
{
ifstream fin(filename);
if (fin.fail())
cout << "Unable to open or read file " << filename << "." << endl;
int count = 0;
while (fin >> grades[count])
{
count++;
}
fin.close();
return count;
}
float computeAverage(int count, float grades[])
{
float average = 0;
int sum;
for (int i = 0; grades[i]; i++)
{
sum += grades[i];
}
average = sum / count;
return average;
}
void display(float average)
{
cout << "The average grade on the midterm is " << average << "%" << endl;
}
int main()
{
cout.setf(ios::fixed);
cout.precision(1);
char filename[256];
float average;
float grades[100];
int count;
getFileName(filename);
readFile(filename, grades);
computeAverage(count, grades);
display(average);
return 0;
}