-5

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;
}
  • those kind of problems are solved by step-by-step debugging in IDE – Alexey Andronov Nov 22 '19 at 03:01
  • 3
    The shown code displays a variable called `average` that's declared in your `main()` as the average value you're asking about. Can point your finger to the exact line of your program that sets the value of this `average` variable to the computed value. This is a trick question, because there is no such line in your program, hence your problem. Oh, you may have other variables in your program called average, but they are completely and totally irrelevant. You forgot the Golden Rule Of Computer Programming: a computer always does exactly what you tell it to do, and not what you want it to do. – Sam Varshavchik Nov 22 '19 at 03:13

1 Answers1

1

There are a lot of bugs here, but the one you are asking about here is this line:

computeAverage(count, grades);

You don't actually save the output of this function:

average = computeAverage(count, grades);

Other Bugs:

for (int i = 0; grades[i]; i++)
{
 sum += grades[i];
}

grades is uninitialized, so it's not guarantee to be 0 in the remaining part of the array.

Instead, since you have the count anyway, just control the loop by the count:

for (int i = 0; i<count; i++)

int sum;

sum is unitialized, meaning it's not guaranteed to be 0. Do this instead:

int sum = 0;// force it to be 0

while (fin >> grades[count])
{
   count++;
}

What happens if you have more than 100 grades? Sure, that's a lot of grades, but it is a good idea to make your code smart enough to handle this. The best way to do this is send a max_size:

int readFile(char filename[], float grades[], int max_size)
{
    //...
    int count = 0;
    while (count < max_size && fin >> grades[count])
    {
        count++;
    }
    //...
}

Finally, you should probably set a maximum for std::cin, so you don't overrun your filename buffer by accident:

#include <iomanip>
//...
void getFileName(char filename[], int size)
{
cout << "Please specify the file name: ";
cin >> setw(size) >> filename;
}