C++ vectors, iterators, structures STL ADDITIONAL CRITERIA: Crt list of structur
ID: 3854486 • Letter: C
Question
C++ vectors, iterators, structures STL
ADDITIONAL CRITERIA:
Crt list of structurs idnticl to dttyp list.
Rplc dt typ with structur nm.
Us itrtors to scn th list in both functions.
Itrtors bhv s pointrs. ccss mmbr vribl using -> oprtor.
To chng it in th functions, pss th list by rfrnc.
STANDARD TEMPLATE LIBRARY A university stores datbse of Students n . dat file called "schoolrecords.dat" with format: FirstName LastName Grdes ID You re tasked to create structure med Student with four-member variables to store prm eters. Main function : a) Declare STL list to store the dat in the file. b.) Red the file 0e line of dat at time c.) Store data in the list. Design two parameterized functions for: A. Removing entry by ID specified by the user Fuction ccepts the ID of an Studet' search the list for the ID d then erase that etry. Fuction call: RemoveEntryForlD(11,5126689); B. Display Studet last me starting with user defined chracter. Function accepts character, search the last mes of Students starting with the user defined chracter d displays etry d list of last mes that strt with that character. Fuction call: LastNameSearch(l1,'s');Explanation / Answer
Here is the program for the question. Post a comment in case of any issues, I shall respond. If happy with the answer, please rate it. Thank you.
Note: Input file format is
Firstname Lastname Grade ID
In case there is any change in the format (question does not give sample data), let me know.
#include <list>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;
typedef struct
{
string firstname;
string lastname;
char grade;
int id;
}Student;
void display(const list<Student> &students);
void search(const list<Student> &students, char letter);
void remove(list<Student> &students, int id);
int main()
{
string filename;
ifstream infile;
Student stud;
list<Student> students;
cout << "Enter input filename: " ;
cin >> filename;
infile.open(filename.c_str());
if(!infile.is_open())
{
cout << "ERROR: could not open file " << filename << endl;
exit(1);
}
while(infile >> stud.firstname)
{
infile >> stud.lastname;
infile >> stud.grade;
infile >> stud.id;
//add the student read from file into the list
students.push_back(stud);
}
infile.close();
int choice = 0;
char start;
int id;
while(choice != 4)
{
cout << "1. Display all" << endl;
cout << "2. Search by lastname" << endl;
cout << "3. Remove by ID" << endl;
cout << "4. Exit" << endl;
cout << "Enter your choice: " ;
cin >> choice;
if(choice == 1)
{
display(students);
}
else if(choice == 2)
{
cout << "Enter the starting char of lastname: " ;
cin >> start;
search(students, start);
}
else if(choice == 3)
{
cout << "Enter Id to be removed : ";
cin >> id;
remove(students, id);
}
else if(choice == 4)
{
break;
}
else
{
cout << "Invalid menu choice!" << endl;
}
cout << "*****" << endl;
}
}
void display(const list<Student> &students)
{
cout << " List of students is " << endl;
Student s;
cout << left << setw(10) << "ID" << setw(15) << "FirstName" << setw(15) << "LastName" << setw(5) << "Grade" << endl;
for(list<Student>::const_iterator it = students.begin(); it != students.end(); ++it)
{
s = *it;
cout << left << setw(10) << s.id << setw(15) << s.firstname << setw(15) << s.lastname << setw(5) << s.grade << endl;
}
}
void search(const list<Student> &students, char letter)
{
cout << " Searching for students with lastname starting from '" << letter << "'" << endl;
Student s;
cout << left << setw(10) << "ID" << setw(15) << "FirstName" << setw(15) << "LastName" << setw(5) << "Grade" << endl;
for(list<Student>::const_iterator it = students.begin(); it != students.end(); ++it)
{
s = *it;
if(s.lastname.find(letter) == 0)
cout << left << setw(10) << s.id << setw(15) << s.firstname << setw(15) << s.lastname << setw(5) << s.grade << endl;
}
}
void remove(list<Student> &students, int id)
{
cout << " Trying to remove student with id = " << id << endl;
Student s;
for(list<Student>::iterator it = students.begin(); it != students.end(); ++it)
{
s = *it;
if(s.id == id) //found a match
{
students.erase(it);
cout << "Removed student with Id " << id << endl;
return;
}
}
cout << "Did not find student with Id " << id << endl;
}
sample input file : stud.txt
John Smith A 111
Michael Jones B 222
Joseph Taylor A 333
output
$ g++ studentlist.cpp
$ ./a.out
Enter input filename: stud.txt
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 1
List of students is
ID FirstName LastName Grade
111 John Smith A
222 Michael Jones B
333 Joseph Taylor A
*****
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 2
Enter the starting char of lastname: J
Searching for students with lastname starting from 'J'
ID FirstName LastName Grade
222 Michael Jones B
*****
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 3
Enter Id to be removed : 222
Trying to remove student with id = 222
Removed student with Id 222
*****
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 1
List of students is
ID FirstName LastName Grade
111 John Smith A
333 Joseph Taylor A
*****
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 3
Enter Id to be removed : 444
Trying to remove student with id = 444
Did not find student with Id 444
*****
1. Display all
2. Search by lastname
3. Remove by ID
4. Exit
Enter your choice: 4
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.