Computing Grade for a course

So my program is supposed to calculate the course grade by finding the average.

ex.
Input
1
2
3
4
Lady Gaga 10 7 7 0 8 2 3 4 8 10
Mary Johnson 9 8 9 9 8 10 6 5 9 8
Robert Park 0 0 0 0 0 0 0 0 0 0
Al Robertson 10 10 10 10 10 10 10 10 10 10

Output
1
2
3
4
Lady Gaga 10 7 7 0 8 2 3 4 8 10 (5.9)
Mary Johnson 9 8 9 9 8 10 6 5 9 8 (8.1)
Robert Park 0 0 0 0 0 0 0 0 0 0 (0)
Al Robertson 10 10 10 10 10 10 10 10 10 10 (10)

(without Parenthesis)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>                                                             
#include <fstream> 
#include <string>
using namespace std;

void computeGrade(ifstream& fin, ofstream& fout);

int main()
{
   ifstream fin;
   ofstream fout;
   
   fin.open("input.txt");
   
   if(fin.fail())
   {
      cout << "Error Opening Input Files\n";
      system("PAUSE");
      exit(1);
   }
   fout.open("output.txt");
   if(fout.fail())
   {
      cout << "Error Opening Output Files\n";
      system("PAUSE");
      exit(1);
   }
   computeGrade(fin, fout);
   
   fout.close();
   fin.close();
   
   system("PAUSE");
   return 0;
}
void computeGrade(ifstream& fin, ofstream& fout)
{
   int i, count; 
   string first, last;
   double average, a[10], sum=0;
   
   fin >> first;
   while(fin)
   {
      fin >> last;
      count = 0; 
      for(i=0; i<10; i++)
      {
         fin >> a[i];
         sum = sum + a[i];
         count++;
      }
      fout << first << " " << last << " ";
      for(i=0; i<10; i++)
      {
         fout << a[i] << " ";
      }
      average = sum / count;  
      fout << average << endl;
      fin >> first; 
   }
}


But my code Output these:
1
2
3
4
Lady Gaga 10 7 7 0 8 2 3 4 8 10 5.9
Mary Johnson 9 8 9 9 8 10 6 5 9 8 14
Robert Park 0 0 0 0 0 0 0 0 0 0 14
Al Robertson 10 10 10 10 10 10 10 10 10 10 24


What is Wrong with this code??
Last edited on
You do not reset your sum between iterations.
Topic archived. No new replies allowed.