I have viewed a lot of threads out there relating to what I am looking for and can't seem to find one that will help. I am reading data in from a file. Even though I already know how many names are stored in the file, I need to write this program to average from an unknown number in the data file. The entire program works except for my float getAverageGrade function. I have tried everything and it will only print out -NaN. Any help is greatly appreciated!!!
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
using namespace std;
void getLetterGrade(float grade[], char lettergrade[], int count);
float getAverageGrade (float grade[], int count);
void printResult (float grade[], string name[], char lettergrade[], int count, \
float average);
int main ()
{
float average;
char lettergrade[50];
string name[50];
float grades[50];
int count=0;
ifstream fp;
fp.open ("grades2.dat");
if (!fp)
{
cout<<"Error\n";
return 0;
}
while (fp.eof() == false)
{
fp>>name[count];
fp>>grades [count];
count++;
}
getLetterGrade(grades, lettergrade, count);
getAverageGrade (grades, count);
printResult (grades, name, lettergrade, count, average);
fp.close();
return 0;
}
void getLetterGrade (float grades[], char lettergrade[], int count)
{
for (int i=0; i<count; i++)
{
if (grades[i]>=90)
lettergrade[i]='A';
else if (grades[i]>=80)
lettergrade[i]='B';
else if (grades[i]>=70)
lettergrade[i]='C';
else if (grades[i]>=60)
lettergrade[i]='D';
else
lettergrade[i]='F';
}
}
float getAverageGrade (float grades[], int count)
{
float sum;
float average;
for (int i=0; i<count; i++)
{
sum += grades[i];
}
average=sum/i; //I have tried this in the loop and in a separate loop and I am having no success!
}
void printResult (float grades[], string name[], char lettergrade[], int count,\
float average)
{
for (int i=0; i<count; i++)
}