Student Grades and Records Problem

The problems of my concerns regarding the code below are the ff:
1. There is always having a logical error in computing for the average of the students

2. Whenever I display records in the data, any type of approach that I do to solve this problem always ends up in a compilation error.

Can someone help me and fix the code? The easier logic that can be done to solve this code, the better. Thanks.

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
#include <iostream>

using namespace std;

class Students
{
    public:
    int ID;
    char Name [100];
    
    double grade1;
    double grade2;
    double grade3;
    double sum;
    double average;
    
}s[0];

void record()
{
    int i; //
    int students; //
    
    system ("clear");
        cout << "Enter number of students: ";
        cin >> students;
        
        
        for (i = 0; i < students; i++)
        {
            cout << "\nEnter ID: ";
            cin >> s[i].ID;
            cin.ignore ();
            cout << "Enter Name: ";
            cin.get (s[i].Name,100);
            
            cout << "Enter Grade 1: ";
            cin >> s[i].grade1;
            
            cout << "Enter Grade 2: ";
            cin >> s[i].grade2;
            
            cout << "Enter Grade 3: ";
            cin >> s[i].grade2;
            
            
            
            s[i].sum = s[i].grade1 + s[i].grade2 + s[i].grade3;
            s[i].average = s[i].sum/3;
            
            cout << "Average: " << s[i].average << endl;
            cin.ignore ();
            cout << "Press any key to continue...";
            cin.get();
            break;
            
         
            
            
            
        }
}

int main()
{
    int opt; //variable
    
    
    
    do {
    cout << "1. Add Student Record\n";
    cout << "2. Display Student Record\n";
    cout << "3. Exit\n";
    cout << "Enter and option: ";
    cin >> opt;
    
    switch (opt)
    {
       
           case 1:
           record();
           break;
           
              //case 2: How to display this? The display list format that I should make for example:  
            //              ID: 1234534214
            //              Name: Andy Stone
            //              Grade 1: 98.22
            //              Grade 2: 76.5
            //              Grade 3: 67.2
            
            default: 
            cin.ignore();
            cout <<"Invalid option! ";
            cout << "Press any key to continue...";
            cin.get();
            system ("clear");
        
        
    }
    
    } while (opt != 3);

    return 0;
} 

Last edited on
1
2
            cout << "Enter Grade 3: ";
            cin >> s[i].grade2;


Whoops!
Perhaps something like this:

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
#include <iostream>

using namespace std;

constexpr size_t MaxStud {10};
constexpr size_t MaxStr {100};

struct Students {
    int ID {};
    char Name[MaxStr] {};

    double grade1 {};
    double grade2 {};
    double grade3 {};
    double sum {};
    double average {};
};

Students s[MaxStud] {};

size_t record() {
    size_t students {};

    cout << "Enter number of students (maximum of " << MaxStud << "): ";
    cin >> students;

    if (students > MaxStud)
        students = MaxStud;

    for (size_t i {}; i < students; ++i) {
        cout << "\nEnter ID: ";
        cin >> s[i].ID;
        cin.ignore();

        cout << "Enter Name: ";
        cin.get(s[i].Name, MaxStr);

        cout << "Enter Grade 1: ";
        cin >> s[i].grade1;

        cout << "Enter Grade 2: ";
        cin >> s[i].grade2;

        cout << "Enter Grade 3: ";
        cin >> s[i].grade3;
        cin.ignore();

        s[i].sum = s[i].grade1 + s[i].grade2 + s[i].grade3;
        s[i].average = s[i].sum / 3;

        cout << "Average: " << s[i].average << '\n';
        cout << "Press any key to continue...";
        cin.get();
    }

    return students;
}

int main() {
    unsigned opt {};
    size_t studs {};

    do {
        cout << "1. Add Student Record\n";
        cout << "2. Display Student Record\n";
        cout << "3. Exit\n";
        cout << "Enter and option: ";
        cin >> opt;
        cin.ignore();

        switch (opt) {
            case 1:
                studs = record();
                break;

            case 2:
                for (size_t i {}; i < studs; ++i) {
                    cout << "ID: " << s[i].ID << '\n';
                    cout << "Name: " << s[i].Name << '\n';
                    cout << "Grade 1: " << s[i].grade1 << '\n';
                    cout << "Grade 2: " << s[i].grade2 << '\n';
                    cout << "Grade 3: " << s[i].grade3 << "\n\n";
                }
                break;

            case 3:
                break;

            default:
                cout << "Invalid option! ";
                cout << "Press any key to continue...";
                cin.get();
        }

    } while (opt != 3);
}

Topic archived. No new replies allowed.