-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
134 lines (113 loc) · 3.58 KB
/
main.cpp
File metadata and controls
134 lines (113 loc) · 3.58 KB
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
#include <bits/stdc++.h>
using namespace std;
// Constants
const int MAX_STUDENTS = 100;
const int SUBJECT_COUNT = 3;
// Student Data
struct Student {
int ID;
string Name;
char Gender;
int Age;
int Marks[SUBJECT_COUNT];
float Average;
char Grade;
};
// Function prototypes
float Calculate_Average(int Marks[], int Size);
char Calculate_Grade(float Average);
void Input_one_Student(Student &Cur_Student);
void Input_Students(Student Students[], int &Count);
void Display_Header();
void Display_Student(Student Cur_Student);
void Display_All_Students(Student Students[], int Count);
void Run_Student_System();
// Function Definitions
// Calculate Average
float Calculate_Average(int Marks[], int Size) {
float sum = 0;
for (int i = 0; i < Size; ++i) {
sum += Marks[i];
}
return sum / Size;
}
// Calculate Grade
char Calculate_Grade(float Average) {
if (Average >= 90) return 'A';
else if (Average >= 80) return 'B';
else if (Average >= 70) return 'C';
else if (Average >= 60) return 'D';
else return 'F';
}
// Input one student
void Input_one_Student(Student &Cur_Student) {
cout << "Enter Student ID: ";
cin >> Cur_Student.ID;
cin.ignore(); // Clear the newline character
cout << "Enter Student Name: ";
getline(cin, Cur_Student.Name);
cout << "Enter Gender (M/F): ";
cin >> Cur_Student.Gender;
cout << "Enter Age: ";
cin >> Cur_Student.Age;
cout << "Enter Marks for " << SUBJECT_COUNT << " Subjects:" << endl;
for (int i = 0; i < SUBJECT_COUNT; ++i) {
cout << "Subject " << i + 1 << ": ";
cin >> Cur_Student.Marks[i];
}
// Calculate Average and Grade
Cur_Student.Average = Calculate_Average(Cur_Student.Marks, SUBJECT_COUNT);
Cur_Student.Grade = Calculate_Grade(Cur_Student.Average);
}
// Input multiple students
void Input_Students(Student Students[], int &Count) {
char choice;
do {
cout << "Enter Student Data #" << (Count + 1) << endl;
Input_one_Student(Students[Count]);
Count++;
if (Count >= MAX_STUDENTS) {
cout << "Reached Maximum Student Capacity" << endl;
break;
}
cout << "Add another student? (Y/N): ";
cin >> choice;
} while (choice == 'Y' || choice == 'y');
}
// Display table header
void Display_Header() {
cout << left << setw(5) << "ID" << setw(15) << "Name" << setw(8) << "Gender" << setw(5) << "Age";
for (int i = 0; i < SUBJECT_COUNT; ++i) {
cout << setw(6) << "M" << (i + 1);
}
cout << setw(8) << "Avg" << setw(6) << "Grade" << endl;
cout << "----------------------------------------------------------" << endl;
}
// Display single student
void Display_Student(Student Cur_Student) {
cout << left << setw(5) << Cur_Student.ID << setw(15) << Cur_Student.Name << setw(8) << Cur_Student.Gender << setw(5) << Cur_Student.Age;
for (int i = 0; i < SUBJECT_COUNT; ++i) {
cout << setw(6) << Cur_Student.Marks[i];
}
cout << fixed << setprecision(2) << setw(8) << Cur_Student.Average << setw(6) << Cur_Student.Grade << endl;
}
// Display all students
void Display_All_Students(Student Students[], int Count) {
cout << "\n===== Student Records =====\n";
Display_Header();
for (int i = 0; i < Count; ++i) {
Display_Student(Students[i]);
}
}
// Control function
void Run_Student_System() {
Student Students[MAX_STUDENTS];
int Student_Count = 0;
Input_Students(Students, Student_Count);
Display_All_Students(Students, Student_Count);
}
// Main function
int main() {
Run_Student_System();
return 0;
}