-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyExampleProgram.cpp
More file actions
85 lines (70 loc) · 2 KB
/
MyExampleProgram.cpp
File metadata and controls
85 lines (70 loc) · 2 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
#include <iostream>
#include <cstring>
using namespace std;
struct Student {
int id;
char name[50];
float gradeAverage;
};
void addStudent(Student *students[], int &numStudents) {
Student *newStudent = new Student;
cout << "Student ID: ";
cin >> newStudent->id;
cout << "Student Name: ";
cin.ignore();
cin.getline(newStudent->name, 50);
cout << "Grade Average: ";
cin >> newStudent->gradeAverage;
students[numStudents] = newStudent;
numStudents++;
}
void deleteStudent(Student *students[], int &numStudents) {
int idToDelete;
cout << "ID of student to delete: ";
cin >> idToDelete;
for (int i = 0; i < numStudents; i++) {
if (students[i]->id == idToDelete) {
delete students[i];
for (int j = i; j < numStudents - 1; j++) {
students[j] = students[j+1];
}
numStudents--;
cout << "Student deleted." << endl;
return;
}
}
cout << "Student not found." << endl;
}
void listStudents(Student *students[], int numStudents) {
for (int i = 0; i < numStudents; i++) {
cout << students[i]->id << "\t" << students[i]->name << "\t" << students[i]->gradeAverage << endl;
}
}
int main() {
Student *students[100];
int numStudents = 0;
int choice;
while (true) {
cout << endl << "1- Add Student" << endl;
cout << "2- Delete Student" << endl;
cout << "3- List Students" << endl;
cout << "4- Quit" << endl;
cout << "Enter choice: ";
cin >> choice;
switch (choice) {
case 1:
addStudent(students, numStudents);
break;
case 2:
deleteStudent(students, numStudents);
break;
case 3:
listStudents(students, numStudents);
break;
case 4:
return 0;
default:
cout << "Invalid choice." << endl;
}
}
}