In: Computer Science
c++ Create a program that creates a sorted list from a data file. The program will prompt the user for the name of the data file. Create a class object called group that contains a First Name, Last Name, and Age. Your main() function should declare an array of up to 20 group objects, and load each line from the input file into an object in the array. The group class should have the following private data elements: first name ,last name age The group class should have the following functions: Constructor(s) Destructor - optional Get - read first name, last name, and age from an input stream Put - write last name, first name, and age to an output stream The group class should have the following operators: > < == compare the group to another group, using Last name, First name, then age, and return a bool Your program should do the following: Prompt the user for the name of the file Open the file Read the data into the array of group objects (maximum size 20) Close the file Sort the array Display the array ============== text file Ann ember 70 jacob Mark 68 David smith 45 Frank lee 37 John doe 30 Kathleen honor 34 bob ember 42 bob ember 13 Richard start 47 Susan hox 36 Expert Answer
PLEASE GIVE THUMBS UP, THANKS
SAMPLE OUTPUT:
CODE :
#include<iostream>
#include<fstream>
using namespace std;
//GROUP CLASS
class group
{
//PRIVATE MEMBERS
private:
string fname;
string lname;
int age;
public:
//FUNCTIONS
void Get(ifstream &in)
{
if(in)
{
in>>fname>>lname>>age;
}
}
void Put()
{
cout<<fname<<" "<<lname<<"
"<<age<<endl;
}
int operator <(group
&A)
{
if(age<A.age)
return 1;
else
return 0;
}
int operator >(group
&A)
{
if(age>A.age)
return 1;
else
return 0;
}
int operator ==(group &A)
{
if(age==A.age)
return 1;
else
return 0;
}
void setfname(string f)
{
fname=f;
}
void setlname(string f)
{
lname=f;
}
void setage(int i)
{
age=i;
}
string getfname()
{
return
fname;
}
string getlname()
{
return
lname;
}
int getage()
{
return
age;
}
};
// A function to implement sort
void Sort(group arr[], int n)
{
int i, j;
for (i = 0; i < n-1; i++)
// Last i elements are already in place
for (j = 0; j < n-i-1; j++)
if (arr[j].getage()<arr[j+1].getage())
{
group
temp;
temp.setfname(arr[j].getfname());
temp.setlname(arr[j].getlname());
temp.setage(arr[j].getage());
arr[j].setfname(arr[j+1].getfname());
arr[j].setlname(arr[j+1].getlname());
arr[j].setage(arr[j+1].getage());
arr[j+1].setfname(temp.getfname());
arr[j+1].setlname(temp.getlname());
arr[j+1].setage(temp.getage());
}
}
int main()
{
group D[20];
int n=0;
ifstream infile;
string filename;
cout<<"Enter Filename: ";
cin>>filename;
infile.open(filename.c_str());
if(!infile)
{
cout<<"Unable to open
file"<<endl;
return 0;
}
while(infile)
{
D[n].Get(infile);
n++;
}
Sort(D,n);
for(int i=0; i<n; i++)
{
D[i].Put();
}
}