-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtask2.cpp
More file actions
136 lines (117 loc) · 3.83 KB
/
task2.cpp
File metadata and controls
136 lines (117 loc) · 3.83 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
#include <iostream>
#include <cstring>
struct Course {
std::string course_code;
std::string course_name;
};
struct Grade {
int mark;
char the_grade;
};
struct Student {
std::string reg_number;
std::string name;
int age;
Course course;
Grade grades;
bool grades_calculated;
};
char calculate_grade(int mark) {
if (mark > 69) {
return 'A';
} else if (mark > 59) {
return 'B';
} else if (mark > 49) {
return 'C';
} else if (mark > 39) {
return 'D';
} else {
return 'E';
}
}
void add_student(Student students[], int& num_students, int max_students) {
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(Student students[], int num_students) {
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(Student& student) {
std::cout << "Enter marks: ";
std::cin >> student.grades.mark;
student.grades.the_grade = calculate_grade(student.grades.mark);
student.grades_calculated = true;
std::cout << "Marks and grades added successfully!" << std::endl;
}
int main() {
const int max_students = 40;
Student students[max_students];
int num_students = 0;
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;
std::string reg_number;
switch (choice) {
case 1:
add_student(students, num_students, max_students);
break;
case 2:
editStudent(students, num_students);
break;
case 3:
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) {
if (!students[i].grades_calculated) {
addMarksAndCalculateGrade(students[i]);
} else {
std::cout << "Grades already calculated for this student!" << std::endl;
}
break;
}
}
break;
case 4:
std::cout << "Exiting program.\n";
break;
default:
std::cout << "Invalid choice. Please try again.\n";
}
} while (choice != 4);
return 0;
}