using functions for file streams

My program is supposed to take an infile and calculate the average of scores for each student and output to an outputfile. It compiles. But after running the program, when I open the output nothing is there. I checked the infile text file and all the names and scores are there. Any thoughts as to why the names and scores and averages are not showing up in an output file?
Here are the exact instructions:
Write a program to compute numeric grades for a course.
The course records are in a file that will serve as the input file. The input file is in the following format:
• Each line contains a student’s last name, then one space, then the student’s first name, then one space, then ten quiz scores all on one line. (The quiz scores are whole numbers and are separated by one space.)

Your program will take its input from this file and send its output to a second file.
• The data in the output file will be the same as the data in the input file
• except that there will be one additional number (of type double ) at the end of each line.
• This number will be the average of the student’s ten quiz scores.
• Use at least one function that has file streams as all or some of its arguments.


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
63
64
65
  #include <iostream>
#include <fstream>
#include<string>

using namespace std;
void calculate(ifstream&, ofstream&);
int main()
{

ifstream input;
ofstream output;

input.open("inputcourserecord.txt");

if(input.fail())
{ 
    cout<<"There was an error while trying to open the course records file."<< endl;

    return 1;
}

input.close();

output.open("outputcourserecord.text");

calculate(input, output);

output.close();

return 0;
}

void calculate(ifstream& input, ofstream& output)
{


double total = 0;
double a[10];
string first;
string last;
double average;

input>>first;

while(input)
{
     input>>last;
  
     for(int i=0;i<10;i++)
    {
        input>> a[i];
        total= total + a[i];
     }

   output<<first<<" "<<last<<" ";
    for(int i=0;i<10;i++)
   {
     output<<a[i]<<" ";

     average=total/10;
    output<<average<<endl;
    input>>first;
  } 
}


Thanks for any help.
closed account (o3hC5Di1)
Hi there,

At line 22 you do: input.close();. You need your input file to remain open while you read from it.

All the best,
NwN
Last edited on
Hi Thanks, I moved that line down to just above line 28 and ran the code again. I checked my output file and it is still empty. I was assuming that just above the output.close() should be where I write the input.close()......Does input.close belong somewhere else in the code?

Thanks.
Okay I got it to work. BUt now I have a problem with what is in the output file. Basically it is supposed to reflect what is in the input file(last name, first name, 10 scores(all separated by one space). Each student is on a separate line.

The output file should show the same information but also have the average at the end of each line for each student.

This is what output file currently looks like:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 SmithKylie1919
  2019
  1819
  2019
  1919
  1719
  2019
  1819
  1919
  2019
  18202036.1
  036.1
  1836.1
  2036.1
  1936.1
  1736.1
  2036.1
  1836.1
  1936.1
  2036.1


here is my input file (for reference):
Smith Kylie 19 20 18 20 19 17 20 18 19 20
Johnson Aaron 20 18 20 20 20 19 17 18 20 20
Williams Craig 20 18 19 19 19 18 20 20 20 19
Jones Logan 17 19 18 19 17 18 20 19 20 17
Howard Chole 20 19 18 18 19 19 19 20 20 17
Miller Olivia 18 19 18 18 20 19 20 19 19 19
Davis Lincoln 20 20 19 19 20 20 20 19 19 20
Jackson Robert 19 18 18 19 19 19 17 19 20 18
Martin Sophia 20 20 19 19 19 18 18 18 19 20
Clark Rory 20 20 20 20 19 19 20 19 19 20
Harris Daniel 19 18 18 18 19 20 20 20 20 19
Hall Ella 20 20 19 19 18 19 19 20 20 19
Young Zoe 19 19 19 18 20 19 20 18 19 20
Scott Luke 20 20 20 19 19 19 20 18 18 20
Thompson Alex 19 19 18 18 20 18 20 20 19 19

Okay I altered my program some here:

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 #include<iostream>
  #include<fstream>
  #include<string>
 
 using namespace std;

  void scoreCalculate(ifstream&, ofstream&); //This function is used to calculate th    e scores and average for each person
 
  int main()
  {
          ifstream input;
          ofstream output;
 
          input.open("inputcourserecord.txt");
 
          if(input.fail())
          {
                  cout<< "There was an error whilr trying to open the course record     file."<<endl;
                 return 1;
          }
 
          output.open("outputcourserecord.text");
 
          scoreCalculate(input, output);
 
          output.close();
          input.close();
 
          return 0;
  }
 
  void scoreCalculate(ifstream& input, ofstream& output)
  {
          int i;
          double total = 0;
          double a[10];
          string first;
          string last;
          double average;
 
          input>>first;
 
          while(input)
          {
                  //total = 0;
   input>>last;
 
                  i=0;
 
                  while(i<10)
                  {
                          input>>a[i];
                          total = total + a[i];
                          i++;
                  }
 
                  output<<first<<" "<<last<<" ";
 
                  i=0;
 
                  while(i<10)
                  {
                          output<< a[i]<<" ";
                          i++;
                  }
 
                          average = total/10;
 
                          //out.set(ios::fixed);
                          //out.set(ios::showpoint);
                          //out.precision(2);
 
                          output<< average<< endl;
                          input>>first;
 
          }
  }


Here is the output file results:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
 Smith Kylie 19 20 18 20 19 17 20 18 19 20 19   //this one is missing an average 
  2 Johnson Aaron 20 18 20 20 20 19 17 18 20 20 38.2 //I am not sure what is going on with the last number in each of these , but it should be the average of the 10 scores for each student.
  3 Williams Craig 20 18 19 19 19 18 20 20 20 19 57.4
  4 Jones Logan 17 19 18 19 17 18 20 19 20 17 75.8
  5 Howard Chole 20 19 18 18 19 19 19 20 20 17 94.7
  6 Miller Olivia 18 19 18 18 20 19 20 19 19 19 113.6
  7 Davis Lincoln 20 20 19 19 20 20 20 19 19 20 133.2
  8 Jackson Robert 19 18 18 19 19 19 17 19 20 18 151.8
  9 Martin Sophia 20 20 19 19 19 18 18 18 19 20 170.8
 10 Clark Rory 20 20 20 20 19 19 20 19 19 20 190.4
 11 Harris Daniel 19 18 18 18 19 20 20 20 20 19 209.5
 12 Hall Ella 20 20 19 19 18 19 19 20 20 19 228.8
 13 Young Zoe 19 19 19 18 20 19 20 18 19 20 247.9
 14 Scott Luke 20 20 20 19 19 19 20 18 18 20 267.2
 15 Thompson Alex 19 19 18 18 20 18 20 20 19 19 286.2
Okay nevermind...I figured it out.
Topic archived. No new replies allowed.