Creating a running total from input file

I'm trying to create a running total that can calculate the weighted average of the numbers in each course. It will add up all the test than divide by the total number of test. The problem I have is that the first course prints out with the average but the other don't. Also, how would I make the grades weighted to get the final average?

Test 1 = 10%
Test 2 = 15%
Test 3 = 20%
Test 4 = 25%
Test 5 = 30%

Input:
Nancy Peterson
22
510 Tumble Dr., San Gabriel, TX 57981
3
(666) 759 - 2249
492-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7
Jacob Dillion
19
951 Moore St., San Marcos, Tx 77688
4
(555) 759 - 2249
555-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7
John Doe
19
951 Moore St., San Marcos, Tx 77688
4
(555) 759 - 2249
555-35-5984
CS1428
80.9
90.2
98.5
89.8
97.7
CS2308
50.9
55.2
60.5
65.8
45.7
CS2308
60.9
55.2
60.5
65.8
60.7

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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  #include <iostream>

#include <fstream>

#include <string>

#include<iomanip>

using namespace std;



int main()

{

    string name, social, address, telephone, course, header;

    int n, age, years;

    double test;

    double total;

    double average;

    const int NUMBER_OF_COURSES = 3;

    const int NUMBER_OF_SCORES = 5;

    int min_students = 1,

        max_students = 100;

    //number of students

    cout << "Enter how many students do you want to see:" << endl;

    cin >> n;

    // Validate the input while loop

    while (n < min_students || n > max_students )

    {

        cout << "You should have at least "<< min_students << " student"
        << " and more than " << max_students << " students" << endl;

        //get number of students again

        cout << "Enter how many students do you want to see:";

        cin >> n;

    }

    //Open Input and Output file

    ifstream fin;

    fin.open("Input.txt");

    if (!fin) {

        cout << "could not open file" << endl;
fin.ignore();
        return -1;

    }

    ofstream fout;


    fout.open("Output.txt");

    if (!fout) {

        cout << "could not open file" << endl;

        return -1;

    }


    // Ask for some info

    for (int i = 0; i < n; ++i) {

        getline(fin,name);

        fin >> age;

        fin.ignore();

        getline(fin,address);

        fin >> years;

        fin.ignore();

        getline(fin,telephone);

        getline(fin,social);

        fout << name << endl;
        fout << age << endl;
        fout << address << endl;
        fout << years << endl;
        fout << telephone << endl;
        fout << social << endl;


        for (int j = 0; j < NUMBER_OF_COURSES; ++j) {
            fin.ignore();

            getline(fin,course);

            fout << course << endl;


            for (int k = 0; k < NUMBER_OF_SCORES; k++) {

                fin >> test;

                total = total + test;

                fin.ignore();

                 fout << test << endl;

            }

            average = total / NUMBER_OF_SCORES;

            fout << average << endl;

        }

    }

    cout << "Data written to output file." << endl;

    fin.close();
    fout.close();


    return 0;



        }
Last edited on
Is the previous answer not good enough for you?
http://www.cplusplus.com/forum/beginner/250621/
@Duthomhas

I not sure how to apply the weight to calculate the average. I have the average without the different weights right now thats why I asked.
I will respond in your other thread.
Topic archived. No new replies allowed.