Please Do in C++ : Need .cpp and .h files....please provide the correct solution
ID: 3832821 • Letter: P
Question
Please Do in C++ :
Need .cpp and .h files....please provide the correct solution!
Create an application to manipulate an array of student record objects. A student record will consist of a name (first, middle, and last), an ID number (9 numeric digits, cannot be more or less), an address (street, city, state, and 5 digit Zip code), and a phone number (3 digit area code and 7 digit number). The application will support an array of students. The user will be allowed to enter records from the keyboard, sort records by either name (last, first, middle) or by ID, save the records to a disk file (name supplied by user), and read the records from a disk file (name again supplied by user). Create a fixed length string that must check that the length of the string is the required length. The fixed length class should be done as a template with the number of characters as the template argument. From this fixed length string, derive a class to hold digits of a fixed length. Create component classes as necessary to use together to implement the student record class. Use either the array template created in an earlier lab to handle the array or you may use the vector class from the STL to handle the array of student record objects. The maximum number of students will be 25 (it may be less).
Explanation / Answer
/* This is used for importing header files */
#include <iostream>
#include <string>
#include<algorithm>
using namespace std;
/*Define the structure */
struct student {
int id;
string lname;
string fname;
string mname;
string street;
string city;
string state;
int zip;
long long phone;
};
bool checklength(struct student s)
{
if(sizeof(s.id) != 9)
{
return false;
}
else if(sizeof(s.zip) != 5)
{
return false;
}
else if(sizeof(s.phone) != 10)
{
return false;
}
return true;
}
/*funtion to display data*/
void showData(struct student s)
{
cout<<" ID: "<<s.id<<" FIRST NAME: "<<s.fname<<" MIDDLE NAME: "<<s.mname
<<" LAST NAME: "<<s.lname
<<" STREET: "<<s.street<<" CITY: "<<s.city
<<" STATE: "<<s.state<<" ZIPCODE: "<<s.zip<<" PHONE: "<<s.phone;
}
/*Funtion to get data from user*/
struct student setData()
{
struct student s1;
cout<<"Enter Student Id ";
cin>>s1.id;
cout<<"Enter First Name ";
cin>>s1.fname;
cout<<"Enter Middle Name ";
cin>>s1.mname;
cout<<"Enter Last Name ";
cin>>s1.lname;
cout<<"Enter Residential Address ";
cin.ignore(); /*user to clear buffer, so that text including space can be read easily*/
getline(cin,s1.street);
cout<<"Enter City Name ";
cin>>s1.city;
cout<<"Enter State Name ";
cin>>s1.state;
cout<<"Enter Zip Code ";
cin>>s1.zip;
cout<<"Enter Phone No. ";
cin>>s1.phone;
return s1;
}
bool comparename (struct student left , struct student right)
{
return left.fname < right.fname ;
}
bool compareid (struct student left , struct student right)
{
return left.id < right.id ;
}
/*Funtion which will execute first*/
int main() {
// your code goes here
struct student s1[3];
int i;
for(i=0;i<3;i++)
{
s1[i] = setData();
if(checklength(s1[i])!=true)
{
//s1[i] = {0};
cout<<"Please enter proper data ";
}
i --;
}
int n;
cout<<" Enter 1 to sort by first name ";
cout<<" Enter any no. to sort by id ";
cin>>n;
if(n==1)
{
std::sort(s1, s1+3, comparename);
}
else
{
std::sort(s1, s1+3, compareid);
}
cout<<" --SORTED-- ";
for(i=0;i<3;i++)
{
showData(s1[i]);
}
return 0;
}
/*
If found any issue, please use comment section to let me know.
*/