Problem A \"Beefier\" Student Database This homework will carry on our work on t
ID: 3820599 • Letter: P
Question
Problem A "Beefier" Student Database This homework will carry on our work on the student database. A solution file for homework 7 is provided, in case you need it. However, you are not required to use this, as long as your own solution is complete and 100% correct as per the requirements of homework 7 1. Program Tasks ("What do I have to do exactly? In this homework, you will extend your student database in the following way: 1. Split the code into a header (hpp), a source (cpp) and a driver file (containing main0); 2. Perform multiple operations on the student records, described below. 3. Use a basic "menu system" to prompt the user for their choice of operation. These are: a) Add a new record from keyboard input, to be added to the end of the array of student objects in memory, b) Find a record using the last name (print "record not found" if the record does not exist), and display it on the screen. If there are multiple records that have the same last name, you have to display all those that match, c) Display all records from the input file on the screen; d Choose a sorting criterion to sort the records, and send them to the output file and to the screen at the same time. Unlike homework 7, we will be not only sorting on GPA, but we want to have the ability to sort on any field in the record. That is, the output file can be sorted on GPA, or first name, or last name, or ID, or academic level. The user will choose what field he/she wants to sort the records on. e) Quit the program. 2. Input/output specifications: The input file specification is the same as the one from homework 7. See later in this file for an example input file. Keep in mind that in this homework, the input" file will also have to be written to at the end of the program. You have to handle this case accordinglyExplanation / Answer
Here is the code for the question and output shown. Please don't forget to rate the answer if it helped. Thank you very much.
Note: To start with you should have an atleast an empty input.dat file for the program. Without input.dat, the program will exit with error message as indicated in the question.
student.hpp
#ifndef student_h
#define student_h
#include <iostream>
class student
{
private:
int id;
std::string firstname;
std::string lastname;
double gpa;
public:
student();
void setFirstname(std::string name);
void setLastname(std::string name);
void setGPA(double g);
void setID(int id);
std::string getFirstname();
std::string getLastname();
double getGPA();
int getID();
friend std::istream & operator >> (std::istream &in, student &s);
friend std::ostream & operator << (std::ostream &out,const student &s);
};
#endif /* student_h */
student.cpp
#include "student.hpp"
#include <iostream>
student::student( )
{
}
void student::setFirstname(std::string name)
{
firstname = name;
}
void student::setLastname(std::string name)
{
lastname = name;
}
void student::setGPA(double g)
{
gpa = g;
}
void student::setID(int id_in)
{
id = id_in;
}
std::string student::getFirstname()
{
return firstname;
}
std::string student::getLastname()
{
return lastname;
}
double student::getGPA()
{
return gpa;
}
int student::getID()
{
return id;
}
std::istream & operator >> (std::istream &in, student &s)
{
in >> s.id >> s.firstname >> s.lastname >> s.gpa ;
return in;
}
std::ostream & operator << (std::ostream &out,const student &s)
{
out<< s.id << " " << s.firstname << " " << s.lastname << " " << s.gpa ;
return out;
}
main.cpp
#include <iostream>
#include <fstream>
#include "student.hpp"
#define MAX 200
using namespace std;
int loadfile(string fname, student studs[]);
void menu(student studs[], int &n);
void writeToFile(string fname, student studs[], int n);
void print(ostream &out, student studs[], int n);
void find(student studs[], int n, string search);
int main(int argc, const char * argv[]) {
student studs[MAX];
string filename = "input.dat";
int n = loadfile(filename, studs);
cout<<"Loaded "<< n << " records from file " << filename << endl;
print(cout, studs, n);
cout<<endl;
menu(studs, n);
writeToFile(filename, studs, n);
return 0;
}
int loadfile(string fname, student studs[])
{
ifstream infile(fname);
if(infile.fail())
{
cout << "Could not open input file (input.dat). Please check if the file exists " <<fname << endl;
exit(1);
}
int count = 0;
while(infile >> studs[count])
count++;
infile.close();
return count;
}
void print(ostream &out, student studs[], int n)
{
for (int i = 0; i < n; i++)
{
out << studs[i] << endl;
}
cout << endl;
}
void writeToFile(string fname, student studs[], int n)
{
ofstream outfile(fname);
if(outfile.fail())
{
cout << "error writing to file " << fname << endl;
return;
}
//outfile << "ID First Name Last Name GPA"<<endl;
print(outfile, studs, n);
outfile.close();
}
void find(student studs[], int n, string search)
{
cout << "Finding students with last name : " << search << endl;
for(int i = 0 ; i < n; i++)
{
if(studs[i].getLastname() == search)
{
cout << studs[i] << endl;
}
}
cout << endl;
}
void sortID(student studs[], int n)
{
int minIdx;
student temp;
for( int i = 0 ; i < n ; ++i)
{
minIdx = i;
for( int j = i+1; j < n; ++j)
{
if(studs[j].getID() < studs[minIdx].getID())
minIdx = j;
}
if(minIdx != i)
{
//swap
temp = studs[i];
studs[i] = studs[minIdx];
studs[minIdx] = temp;
}
}
}
void sortGPA(student studs[], int n)
{
int minIdx;
student temp;
for( int i = 0 ; i < n ; ++i)
{
minIdx = i;
for( int j = i+1; j < n; ++j)
{
if(studs[j].getGPA() < studs[minIdx].getGPA())
minIdx = j;
}
if(minIdx != i)
{
//swap
temp = studs[i];
studs[i] = studs[minIdx];
studs[minIdx] = temp;
}
}
}
void sortFirstname(student studs[], int n)
{
int minIdx;
student temp;
for( int i = 0 ; i < n ; ++i)
{
minIdx = i;
for( int j = i+1; j < n; ++j)
{
if(studs[j].getFirstname() < studs[minIdx].getFirstname())
minIdx = j;
}
if(minIdx != i)
{
//swap
temp = studs[i];
studs[i] = studs[minIdx];
studs[minIdx] = temp;
}
}
}
void sortLastname(student studs[], int n)
{
int minIdx;
student temp;
for( int i = 0 ; i < n ; ++i)
{
minIdx = i;
for( int j = i+1; j < n; ++j)
{
if(studs[j].getLastname() < studs[minIdx].getLastname())
minIdx = j;
}
if(minIdx != i)
{
//swap
temp = studs[i];
studs[i] = studs[minIdx];
studs[minIdx] = temp;
}
}
}
void menu(student studs[], int &n)
{
int choice = -1;
while(choice != 8)
{
cout << endl;
cout << "========= Menu =========" << endl;
cout << "[1]. Add student" << endl;
cout << "[2]. Find by lastname" << endl;
cout << "[3]. Sort by ID" << endl;
cout << "[4]. Sort by First Name" << endl;
cout << "[5]. Sort by Last Name" << endl;
cout << "[6]. Sort by GPA" << endl;
cout << "[7]. Display all" << endl;
cout << "[8]. Quit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice)
{
case 1:
if(n == MAX)
{
cout << "Array already full ! "<<endl;
}
else
{
cout << "Enter the student details (Input format: ID Firstname Lastname GPA ): ";
cin >> studs[n];
n++;
cout << "Student stored successfully!" <<endl;
}
break;
case 2:
if(n == 0)
cout << "There are no student records !" << endl;
else
{
string search;
cout << "Enter the last name to search : ";
cin >> search;
find(studs, n , search);
}
break;
case 3:
sortID(studs, n);
cout << "The list after sorting by ID is " << endl;
print(cout, studs, n);
break;
case 4:
sortFirstname(studs, n);
cout << "The list after sorting by First name is " << endl;
print(cout, studs, n);
break;
case 5:
sortLastname(studs, n);
cout << "The list after sorting by Last name is " << endl;
print(cout, studs, n);
break;
case 6:
sortGPA(studs, n);
cout << "The list after sorting by GPA is " << endl;
print(cout, studs, n);
break;
case 7:
cout << "The list of students is " << endl;
print(cout, studs, n);
break;
case 8:
break;
default:
cout << "Invalid menu choice!"<<endl;
}
}
}
output
Loaded 0 records from file input.dat
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 1
Enter the student details (Input format: ID Firstname Lastname GPA ): 3 John Smith 7.8
Student stored successfully!
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 1
Enter the student details (Input format: ID Firstname Lastname GPA ): 2 Alice C 9.0
Student stored successfully!
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 1
Enter the student details (Input format: ID Firstname Lastname GPA ): 4 Henry K 8.5
Student stored successfully!
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 7
The list of students is
3 John Smith 7.8
2 Alice C 9
4 Henry K 8.5
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 2
Enter the last name to search : Smith
Finding students with last name : Smith
3 John Smith 7.8
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 1
Enter the student details (Input format: ID Firstname Lastname GPA ): 1 Bob Smith 8.0
Student stored successfully!
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 2
Enter the last name to search : Smith
Finding students with last name : Smith
3 John Smith 7.8
1 Bob Smith 8
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 3
The list after sorting by ID is
1 Bob Smith 8
2 Alice C 9
3 John Smith 7.8
4 Henry K 8.5
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 4
The list after sorting by First name is
2 Alice C 9
1 Bob Smith 8
4 Henry K 8.5
3 John Smith 7.8
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 5
The list after sorting by Last name is
2 Alice C 9
4 Henry K 8.5
1 Bob Smith 8
3 John Smith 7.8
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 6
The list after sorting by GPA is
3 John Smith 7.8
1 Bob Smith 8
4 Henry K 8.5
2 Alice C 9
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 3
The list after sorting by ID is
1 Bob Smith 8
2 Alice C 9
3 John Smith 7.8
4 Henry K 8.5
========= Menu =========
[1]. Add student
[2]. Find by lastname
[3]. Sort by ID
[4]. Sort by First Name
[5]. Sort by Last Name
[6]. Sort by GPA
[7]. Display all
[8]. Quit
Enter your choice: 8
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.