forked from sapai05/cpp-and-datastructures
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstudentlist.cpp
More file actions
92 lines (85 loc) · 1.89 KB
/
studentlist.cpp
File metadata and controls
92 lines (85 loc) · 1.89 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
#include <iostream>
#include <cstring>
#include <vector>
#include <iomanip>
using namespace std;
/* Written by: Sahil Pai
This project is a list that adds students. deletes students, and prints them.
Date: 11/3/2022
*/
// struct to act like global variables
struct student {
char firstName[20];
char lastName[20];
int ID = 0;
float GPA = 0;
};
//add students
void add(vector<student*> &v) {
cout<<"add your students: "<<endl;
student* s = new student();
cout<<"Enter student first name: "<<endl;
cout<<"First name: ";
cin>> s->firstName;
cout<<"Enter student last name: "<<endl;
cout<<"Last name: ";
cin>> s->lastName;
cout<<"Enter student ID: "<<endl;
cout<<"ID: ";
cin>> s->ID;
cout<<"Enter student GPA: "<<endl;
cout<<"GPA: ";
cin>> s->GPA;
cin.ignore(10, '\n');
v.push_back(s);
}
// prints students
void print(vector<student*> &v) {
for (int i = 0; i < v.size(); i++) {
cout<<"First name: ";
cout<< v[i]->firstName<<endl;
cout<<"Last name: ";
cout<< v[i]->lastName<<endl;
cout<<"ID number: ";
cout<< v[i]->ID<<endl;
cout<<"GPA: ";
cout<< fixed << setprecision(2) << v[i] -> GPA <<endl;
cout<< '\n' <<endl;
}
}
// deletes students
void del(vector<student*> &v) {
int enter;
cout <<"Enter student id that you want removed: "<<endl;
cin >> enter;
for (int i = 0; i < v.size(); i++) {
if (v[i]->ID== enter) {
delete v[i];
v.erase(v.begin() + i);
}
}
}
// interface to add, delete, print, quit
int main() {
vector<student*> v;
char input;
bool cont = true;
while (cont == true) {
cout << "Enter 1 to add, 2 to delete, 3 to print, or 4 to quit." << endl;
cin >> input;
if (input == '1') {
add(v);
}
else if (input == '2') {
del(v);
}
else if (input == '3') {
print(v);
}
else if (input == '4'){
exit(0);
} else {
cont == true;
}
}
}