Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a C++ program that will perform sorting and searching across records store

ID: 3870237 • Letter: W

Question

Write a C++ program that will perform sorting and searching across records stored in a file. The program should first query the user for a filename. The program should open the file and read the first entry. It defines the number of records in the file. The program should read each record and store it into memory. Each record in the file will have sets of three lines. Each set consists of: lastname, firstname, and birthdate. Your program should prompt the user for a command: Searching - The user should be able to supply a search value and your program should print any entries that start with that value. You should search all first names, last names, and birthdates. There may be multiple values returned. Sorting - The user should be able to choose to print the data in its original unsorted form, sorted by first name, or sorted by last name. Quit – Exit the program

Explanation / Answer

#include<iostream>
#include <fstream>
#include <string.h>
#include <stdlib.h>

using namespace std;
//Class Person definition
class Person
{
private:
//Data member to store data
string lastName, firstName;
int date;
int size;
public:
//Member functions
void readFile(Person p[]);
void display(Person p[]);
void searchFirstName(Person p[]);
void searchLastName(Person p[]);
void searchBirthDate(Person p[]);
void sortFirstName(Person p[]);
void sortLastName(Person p[]);
void sortBirthDate(Person p[]);
};//End of class

//Function to read PersonalInfo.txt binary file contents and store in data member
void Person::readFile(Person p[])
{
//Creates ifstream class object
ifstream inFile;
//Creates a temporary object
Person pp;

//Opens the file for reading data from binary file PersonalInfo.txt
inFile.open("PersonalInfo.txt", ios::binary | ios::in);
//Read the first line data and store it in size
inFile>>size;
//Loops till size
for(int c = 0; c < size; c++)
{
//Reads data from file and stores it in data member
inFile>>p[c].lastName>>p[c].firstName>>p[c].date;
}//End of for loop
//Close file
inFile.close();
}//End of function

//Function to display the person information
void Person::display(Person p[])
{
//Loops till size
for(int c = 0; c < size; c++)
{
//Displays the data
cout<<"Last Name: "<<p[c].lastName<<" First Name: "<<p[c].firstName<<" Birth Date: "<<p[c].date<<endl;
}//End of for loop
}//End of function

//Function to return user choice for main menu
int mainMenu()
{
//To store user choice
int ch;
//Displays menu
cout<<" 1 Search";
cout<<" 2 Sort";
cout<<" 3 Exit";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function

//Function to return user choice for sub menu
int subMenu()
{
//To store user choice
int ch;
//Displays menu
cout<<" 1 First Name";
cout<<" 2 Last Name";
cout<<" 3 Birth Date";
//Accepts user choice
cout<<" Enter your choice: ";
cin>>ch;
//Returns user choice
return ch;
}//End of function

void Person::searchFirstName(Person p[])
{
//Flag is initially zero
int flag = 0, c;
//To store user entered first name
string name;
//Accepts first name from the user
cout<<" Enter the first name to search: ";
cin>>name;
//Loops till size
for(c = 0; c < size; c++)
{
//Checks if user entered name is equal current object first name
if(name.compare(p[c].firstName) == 0)
{
//Set the flag to one
flag = 1;
//Displays the data
cout<<"Last Name: "<<p[c].lastName<<" First Name: "<<p[c].firstName<<" Birth Date: "<<p[c].date<<endl;
}//End of if
}//End of for loop
//Checks if the flag value is zero then display error message
if(flag == 0)
cout<<" No such record.";
}//End of function


void Person::searchLastName(Person p[])
{
//Flag is initially zero
int flag = 0, c;
//To store user entered last name
string name;
//Accepts last name from the user
cout<<" Enter the last name to search: ";
cin>>name;
//Loops till size
for(c = 0; c < size; c++)
{
//Checks if user entered last name is equal current object last name
if(name.compare(p[c].lastName) == 0)
{
//Set the flat to one
flag = 1;
//Displays the data
cout<<"Last Name: "<<p[c].lastName<<" First Name: "<<p[c].firstName<<" Birth Date: "<<p[c].date<<endl;
}//End of if
}//End of for loop
//Checks if the flag value is zero then display error message
if(flag == 0)
cout<<" No such record.";
}//End of function

void Person::searchBirthDate(Person p[])
{
//Flag is initially zero
int flag = 0, c;
//To store user entered date
int bDate;
//Accepts date from the user
cout<<" Enter birth date to search: ";
cin>>bDate;
//Loops till size
for(c = 0; c < size; c++)
{
//Checks if user entered date is equal current object date
if(p[c].date == bDate)
{
//Set the flag to one
flag = 1;
//Displays the data
cout<<"Last Name: "<<p[c].lastName<<" First Name: "<<p[c].firstName<<" Birth Date: "<<p[c].date<<endl;
}//End of if
}//End of for loop
//Checks if the flag value is zero then display error message
if(flag == 0)
cout<<" No such record.";
}//End of function

//Function to sort first name
void Person::sortFirstName(Person p[])
{
//Loops till size
for(int i = 0; i < size; i++)
{
//Loops till one added to outer loop value to size minus one
for(int j = i+ 1 ; j < size - 1;j++)
{
//Checks if current position first name is greater than the next position first name
if((p[i].firstName.compare(p[j].firstName)) > 0)
{
//Swap first name
string tempq = p[i].firstName;
p[i].firstName = p[j].firstName;
p[j].firstName = tempq;

//Swap last name
string templ = p[i].lastName;
p[i].lastName = p[j].lastName;
p[j].lastName= templ;

//Swap date
int tempd = p[i].date;
p[i].date = p[j].date;
p[j].date = tempd;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function

//Function to sort last name
void Person::sortLastName(Person p[])
{
//Loops till size
for(int i = 0; i < size; i++)
{
//Loops till one added to outer loop value to size minus one
for(int j = i + 1; j < size - 1; j++)
{
//Checks if current position last name is greater than the next position last name
if((p[i].lastName.compare(p[j].lastName)) > 0)
{
//Swap first name
string tempq = p[i].firstName;
p[i].firstName = p[j].firstName;
p[j].firstName = tempq;

//Swap last name
string templ = p[i].lastName;
p[i].lastName = p[j].lastName;
p[j].lastName= templ;

//Swap date
int tempd = p[i].date;
p[i].date = p[j].date;
p[j].date = tempd;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function

//Function to sort date
void Person::sortBirthDate(Person p[])
{
//Loops till size
for(int i = 0; i < size; i++)
{
//Loops till one added to outer loop value to size minus one
for(int j = i + 1; j < size - 1; j++)
{
//Checks if current position date is greater than the next position date
if(p[i].date > p[j].date)
{
//Swap first name
string tempq = p[i].firstName;
p[i].firstName = p[j].firstName;
p[j].firstName = tempq;

//Swap last name
string templ = p[i].lastName;
p[i].lastName = p[j].lastName;
p[j].lastName= templ;

//Swap date
int tempd = p[i].date;
p[i].date = p[j].date;
p[j].date = tempd;
}//End of if
}//End of inner for loop
}//End of outer for loop
}//End of function

//Main function definition
int main()
{
//Creates an array of objects
Person per[10];
//Calls the function to read data from file and store it in data members
per[0].readFile(per);
//To store main choice and sub choice entered by the user
int mainChoice, subChoice;
//Loops till mainChoice value is not 3
do
{
//Displays main menu and stores the user choice
mainChoice = mainMenu();
//Outer switch for main menu
switch(mainChoice)
{
case 1:
//Displays sub menu and stores user choice
subChoice = subMenu();
//Inner switch for sub menu
switch(subChoice)
{
case 1:
per[0].searchFirstName(per);
break;
case 2:
per[0].searchLastName(per);
break;
case 3:
per[0].searchBirthDate(per);
break;
default:
cout<<" Invalid choice.";
}//End of switch
break;
case 2:
//Displays sub menu and stores user choice
subChoice = subMenu();
//Inner switch for sub menu
switch(subChoice)
{
case 1:
per[0].sortFirstName(per);
cout<<" After sorting First name ";
per[0].display(per);
break;
case 2:
cout<<" After sorting Last name ";
per[0].sortLastName(per);
per[0].display(per);
break;
case 3:
cout<<" After sorting Birth Date ";
per[0].sortBirthDate(per);
per[0].display(per);
break;
default:
cout<<" Invalid choice.";
}//End of inner switch
break;
case 3:
exit(0);
default:
cout<<" Invalid choice.";
}//End of outer switch
}while(1);//End of while
}//End of main

Sample Run:


1 Search
2 Sort
3 Exit
Enter your choice: 1

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 1

Enter the first name to search: Pyari
Last Name: Sahu First Name: Pyari Birth Date: 23

1 Search
2 Sort
3 Exit
Enter your choice: 1

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 1

Enter the first name to search: Suresh

No such record.
1 Search
2 Sort
3 Exit
Enter your choice: 1

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 2

Enter the last name to search: Sahu
Last Name: Sahu First Name: Pyari Birth Date: 23
Last Name: Sahu First Name: Ram Birth Date: 20

1 Search
2 Sort
3 Exit
Enter your choice: 1

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 2

Enter the last name to search: Padhi
Last Name: Padhi First Name: Rahim Birth Date: 12

1 Search
2 Sort
3 Exit
Enter your choice: 2

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 1

After sorting First name
Last Name: Sahu First Name: Pyari Birth Date: 23
Last Name: Padhi First Name: Rahim Birth Date: 12
Last Name: Sahu First Name: Ram Birth Date: 20
Last Name: Panda First Name: Rashmi Birth Date: 29
Last Name: Panda First Name: Mamani Birth Date: 11

1 Search
2 Sort
3 Exit
Enter your choice: 2

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 2

After sorting Last name
Last Name: Padhi First Name: Rahim Birth Date: 12
Last Name: Panda First Name: Rashmi Birth Date: 29
Last Name: Sahu First Name: Ram Birth Date: 20
Last Name: Sahu First Name: Pyari Birth Date: 23
Last Name: Panda First Name: Mamani Birth Date: 11

1 Search
2 Sort
3 Exit
Enter your choice: 2

1 First Name
2 Last Name
3 Birth Date
Enter your choice: 3

After sorting Birth Date
Last Name: Padhi First Name: Rahim Birth Date: 12
Last Name: Sahu First Name: Ram Birth Date: 20
Last Name: Sahu First Name: Pyari Birth Date: 23
Last Name: Panda First Name: Rashmi Birth Date: 29
Last Name: Panda First Name: Mamani Birth Date: 11

1 Search
2 Sort
3 Exit
Enter your choice: 3

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote