Question: Can you fix my code: #include #include #include #include using namespace std; ifstream infile; struct Student { string firstName; string lastName; string id; double gpa;

Can you fix my code: #include
#include
#include
#include
using namespace std;
ifstream infile;
struct Student {
string firstName;
string lastName;
string id;
double gpa;
};
class StudentList {
list students;
public:
void addStudent(const Student &student){
students.push_front(student);
}
void deleteStudent(const string &id){
auto it = find_if(students.begin(), students.end(),[&](const Student &s){
return s.id == id;
});
if (it != students.end()){
students.erase(it);
cout << "Student ID: "<< id <<" has been deleted" << endl;
} else {
cout << "Student ID: "<< id <<" not found" << endl;
}
}
void print() const {
for (const auto &student : students){
cout << student.firstName <<""<< student.lastName <<""<< student.id <<""<< student.gpa << endl;
}
cout << endl;
}
Student* findStudent(const string &id){
auto it = find_if(students.begin(), students.end(),[&](const Student &s){
return s.id == id;
});
if (it != students.end()){
return &(*it);
} else {
return nullptr;
}
}
void changeStudent(const string &id, double newGpa){
Student *student = findStudent(id);
if (student){
student->gpa = newGpa;
} else {
cout << "Student ID: "<< id <<" not found" << endl;
}
}
};
void processAdd(StudentList &students){
Student s;
infile >> s.firstName >> s.lastName >> s.id >> s.gpa;
students.addStudent(s);
students.print();
}
void processDelete(StudentList &students){
string id;
infile >> id;
students.deleteStudent(id);
students.print();
}
void processSearch(const StudentList &students){
string id;
infile >> id;
Student *student = students.findStudent(id);
if (student){
cout << "Information Located: "<< student->firstName <<""<< student->lastName <<""<< student->id <<""<< student->gpa << endl;
} else {
cout << "Information for Student ID: "<< id <<" not found." << endl;
}
}
void processChange(StudentList &students){
string id;
double newGpa;
infile >> id >> newGpa;
students.changeStudent(id, newGpa);
students.print();
}
int main(){
char code;
StudentList students;
infile.open("updates.txt");
while (infile >> code){
switch (code){
case 'A':
case 'a':
processAdd(students);
break;
case 'D':
case 'd':
processDelete(students);
break;
case 'S':
case 's':
processSearch(students);
break;
case 'C':
case 'c':
processChange(students);
break;
default:
cout << "Invalid code "<< code << endl;
break;
}
}
infile.close();
return 0;
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!