Displaying Average Mark

I need help with my program for displaying the average mark of student records.
My coding for average mark is on Case 1.
The problem is that it does not display the average marks properly, instead it displays some random int and char values.
Always the "Number of Records" value is always 0.
The default student records are P1001(78.50) and P1002(66). However, I have not set the code to (int j=0;j<2;j++), because later on the program will allow the user to add more student records and marks.

Here is my code:
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
#include <iostream>
#include <string>
using namespace std;

struct StudentRecord
{
       string studentID;
       float studentMark;
       char studentGrade;
} student;
int i;
int totalStudents;
int linearSearch (StudentRecord*students,const string studentID);

int main()
{
    int menu;
    StudentRecord student[] = {
                  {"P1001",78.50,'D'},
                  {"P1002",66,'C'}
                  };
    int totalStudents=0;
    float totalMarks=0;
    float averageMark;
    string id;
    int mark;
    int j;

    cout << "MAIN MENU\n"
         << "0. Exit         1. Statistics\n"
         << "2. Enter mark   3. Find mark\n"
         << "------------------------------\n"
         << "Your choice -> ";
    cin >> menu;
  while (menu != 0)
  {
    switch (menu)
    {
           case 1:
                for(int j=0;j<totalStudents;j++)
                 {
                   totalMarks+=student[j].studentMark;
                   totalStudents++;
                 }
                 averageMark=totalMarks/totalStudents;
                 cout << "Number of records: " << totalStudents <<endl;
                 cout << "Mean or average: " << averageMark <<endl;
                break;
           case 2:
                cout << "Enter a Student Record: " <<endl;
                cout << "Student ID -> ";
                cin >> id;
                break;
           case 3:
                cout << "Find marks for ID -> ";
                cin >> id;
                if(linearSearch(student,id))
                cout << "Student ID: " << student[i].studentID << endl;
                else
                cout << "Record does not exist";

                break;
           default:
                   cout << "Invalid selection. Please make a selection between 0-3.\n"
                        << endl;
                   break;
    }    
system("Pause");
    cout << "MAIN MENU\n"
         << "0. Exit         1. Statistics\n"
         << "2. Enter mark   3. Find mark\n"
         << "----------------------------\n"
         << "Your choice -> ";
    cin >> menu;
  }
 
return 0;
}

int linearSearch(StudentRecord*students,const string studentID)

{
    int retValue(-1);
    for(int i=0;i<totalStudents;++i)
    {
            if(students[i].studentID == studentID)
            {
              retValue=i;
              break;
            }
    }
    return retValue;
}


What am I doing wrong?
what do you think you are doing here???

1
2
3
4
5
for(int j=0;j<totalStudents;j++)
                 {
                   totalMarks+=student[j].studentMark;
                   totalStudents++;
                 }


totalStudent is zero which you set at the beginning, how will it enter the loop..
secondly, even if it will enter some how.. you are incrementing j as well as totalStudent, so in that it will keep on running in the loop for the lifetime...

do this:

1
2
3
4
5
6
7
8
9
//on top do this
int totalStudents=2;//because you have two students.

//then in the switch case do this:
for(int j=0;j<totalStudents;j++)
{
        totalMarks+=student[j].studentMark;
}
averageMark=totalMarks/totalStudents;




I have changed the value of totalStudents to 2 and removed the totalStudents++, and the 'number of records' displayed properly. However, everytime I check the statistics, the averageMark keeps adding to itself. E.g. It displays 72.25 at first. Then when I select the statistics menu again, the average mark changes to 144.5. But the 'number of records' remain at 2.

I am trying to program a code that will display the number of records and the average mark of those records. By default, there are 2 records (P1001, P1002). However, later on I have to allow the user to be able to add more records/marks. And then the statistics should update these records and then re-calculate the average mark.

How can I fix the averageMark problem?
Topic archived. No new replies allowed.