-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask3.cpp
More file actions
151 lines (130 loc) · 4.05 KB
/
task3.cpp
File metadata and controls
151 lines (130 loc) · 4.05 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include <iostream>
#include <string>
class Course {
public:
std::string course_code;
std::string course_name;
};
class Grade {
public:
int mark;
char the_grade;
void calculateGrade() {
if (mark > 69) {
the_grade = 'A';
} else if (mark > 59) {
the_grade = 'B';
} else if (mark > 49) {
the_grade = 'C';
} else if (mark > 39) {
the_grade = 'D';
} else {
the_grade = 'E';
}
}
};
class Student {
public:
std::string reg_number;
std::string name;
int age;
Course course;
Grade grades;
bool grades_calculated;
void addMarksAndCalculateGrade() {
if (!grades_calculated) {
std::cout << "Enter marks: ";
std::cin >> grades.mark;
grades.calculateGrade();
grades_calculated = true;
std::cout << "Marks and grades added successfully!" << std::endl;
} else {
std::cout << "Grades already calculated for this student!" << std::endl;
}
}
};
class StudentSystem {
private:
static const int max_students = 40;
Student students[max_students];
int num_students;
public:
StudentSystem() : num_students(0) {}
void addStudent() {
if (num_students < max_students) {
Student newStudent;
std::cout << "Enter registration number: ";
std::cin >> newStudent.reg_number;
std::cout << "Enter name: ";
std::cin >> std::ws;
std::getline(std::cin, newStudent.name);
std::cout << "Enter age: ";
std::cin >> newStudent.age;
std::cout << "Enter course code: ";
std::cin >> newStudent.course.course_code;
std::cout << "Enter course name: ";
std::cin >> std::ws;
std::getline(std::cin, newStudent.course.course_name);
students[num_students] = newStudent;
num_students++;
std::cout << "Student added successfully!" << std::endl;
} else {
std::cout << "Maximum number of students reached!" << std::endl;
}
}
void editStudent() {
std::string reg_number;
std::cout << "Enter registration number of the student to edit: ";
std::cin >> reg_number;
for (int i = 0; i < num_students; ++i) {
if (students[i].reg_number == reg_number) {
std::cout << "Enter new name: ";
std::cin >> std::ws;
std::getline(std::cin, students[i].name);
std::cout << "Enter new age: ";
std::cin >> students[i].age;
std::cout << "Student details updated successfully!" << std::endl;
return;
}
}
std::cout << "Student not found!" << std::endl;
}
void addMarksAndCalculateGrade() {
std::string reg_number;
std::cout << "Enter registration number of the student: ";
std::cin >> reg_number;
for (int i = 0; i < num_students; ++i) {
if (students[i].reg_number == reg_number) {
students[i].addMarksAndCalculateGrade();
return;
}
}
std::cout << "Student not found!" << std::endl;
}
};
int main() {
StudentSystem studentSystem;
int choice;
do {
std::cout << "\n1. Add a student\n2. Edit student details\n3. Add marks and calculate grades\n4. Exit\n";
std::cout << "Enter your choice: ";
std::cin >> choice;
switch (choice) {
case 1:
studentSystem.addStudent();
break;
case 2:
studentSystem.editStudent();
break;
case 3:
studentSystem.addMarksAndCalculateGrade();
break;
case 4:
std::cout << "Exiting program.\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}