Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_step_2

Step: 3

blur-text-image_step3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions