How to reset the accumulator

So I'm in the process of finishing this program where I need to find the average grades of student 1 and the average grades of student 2 but it's giving me the cumulative average of all their grades. How can I reset the accumulator? Or is there another way to separate this?
Could you show the relevant part of your code so that we see how you do use accumulator?
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
#include <iostream>

using namespace std;

int main()
{
    const int MAX_VALUE = 10;
    const int SENTINEL = 999;

    string teacherName = " ";
    string studentName = " ";
    string className = " ";

    int loopCount = 0;
    int numStudents = 0;
    int numTest = 0;

    double total = 0;
    double testScore = 0;
    double studentAverage = 0;
    double classAverage = 0;



    cout << "Enter the teachers name: ";
    cin >> teacherName;
    cin.ignore(1);
    getline (cin, teacherName);

    cout << "Enter the class designation: ";
    cin >> className;



    cout << "Enter the number of students: ";
    cin >> numStudents;
    while (numStudents < 0)
    {
        cout << "Please enter a positive number: ";
        cin >> numStudents;
    }


    for (int x = 1; x <= numStudents; x++)
    {
        cout << "Enter students name: ";
        cin >> studentName;
        cin.ignore (1);
        getline (cin, studentName);


        for (int y = 0; y < MAX_VALUE; y++)
        {

            cout << "Please enter a number from 0 - 100 or type 999 to stop: ";
            cin >> testScore;


            if (testScore == SENTINEL)
            {
                break;
            }
 
            total = total + testScore;
            loopCount = loopCount + 1;
            studentAverage = total / loopCount;


        }



        cout << studentAverage << endl;
So by "accumulator" you do mean total and loopCount?

Rhetoric question: Why are they correct for the first student?
A: Because you do set them properly before you get the first testScore of the first student.

Followup question: Can you change their value before you get the first testScore of the second/Nth student?
(Hint: What do you do for the second student on lines 46-49?)



Semi-unrelated question. How many averages do you calculate for a student that has 42 testScores?
A: 42
Q: Why? You print only the last one of them on line 73. Perhaps a move of line 66 out and after the loop to line 72?



PS. C had a fixed scope syntax: first variable declarations and then the code. That made it easy for the compiler to reserve space from stack for a function call.
C++ did relax that. A variable declaration does not have to be at the start of a function. It can be close to where the variable is actually used. (The compilers are more complex now and can cope with that.)

You could declare (and initialize) a variable within a loop body, if you don't need it after the loop. Then its lifetime is only that one iteration.
Last edited on
thanks for the help i finally cracked it !!
Topic archived. No new replies allowed.