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++)
}

i goes out of scope (disappears) after your for loop has finished. Divide by count instead

Or change the scope of i . Remember to return a value. And to initialize sum.

float getAverageGrade (float grades[], int count)
{
  float sum = 0;
  float average;
  int i;
  for (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!
  return average;
}

Regarding...

while (fp.eof() == false)

Avoid Loop Control Using eof()

Thanks DS, I totally glossed over it...

Or change the scope of i . Remember to return a value. And to initialize sum.

float getAverageGrade (float grades[], int count)
{
  float sum = 0;
  float average;
  int i;
  for (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!
  return average;
}

Regarding...

while (fp.eof() == false)

Avoid Loop Control Using eof()

.

I initialized sum (sorry that was a beginners error :-() And I am dividing by count, and added the return average, however, I am still getting the -NaN value for average. Argh

Do you have: average = getAverageGrade(grades, count); back in main()?
(you are trying to send it into your output function but if you haven't returned it to main properly from the getAvgGrade then it will contain garbage)

THANKS!!!! That was the kicker! Sorry, I just started learning this 3 months ago so I am bound to make these kind of errors. I really appreciate your help!!!

No prob at all :) Good luck with it.

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.