i. The name of the file must be library.cpp: Note: This program will be submitte
ID: 3726290 • Letter: I
Question
i. The name of the file must be library.cpp: Note: This program will be submitted as part of Assignment 5. To receive full credit for this part, the program must be completed/partially completed during lab time. A library has a sorted list, sorted by last name, of its members in a file with the following information for each member: First name, Last name, Member ID, and Telephone Number The list contains 100 members. Write a program that reads the data from the file and store it in an array of structures. Display the database while reading from file. The program should allow a librarian to retrieve users' information based on last name or member ID search. So, the program should have a menu-driven user interface. In case of the last name search choice, call a function that will search all members that match and display them. Another function should be called for ID search option. If there is no match, a message saying so should be displayed. Which searching algorithm would you choose for each type? Why? Note: passing an array of structures is similar to passing any other type of arrays.Explanation / Answer
#include<iostream>
#include<string>
#include <fstream>
#include<stdlib.h>
#define MAX 100
using namespace std;
// Structure for library created
struct library
{
// Variables to store data
string firstName;
string lastName;
int memberID;
string telephoneNo;
};// End of structure
typedef struct library lib;
// Function to read records from file and stores in library array
int readFile(lib ll[])
{
// ifstream class object declared
ifstream inFile;
// Opens the Library.txt file to read
inFile.open("Library.txt");
int c = 0;
// Loops till end of the file
while(!inFile.eof())
{
// Read data and stores it in the data member
inFile>>ll[c].firstName;
inFile>>ll[c].lastName;
inFile>>ll[c].memberID;
inFile>>ll[c].telephoneNo;
// Increase the record counter by one
c++;
}//End of while
//Close the file
inFile.close();
// Returns number of records in file
return c;
}// End of function
// Function to display all the records
void displayRecord(lib ll[], int len)
{
// Loops till the length
for(int c = 0; c < len; c++)
{
cout<<" Record - "<<(c + 1);
cout<<" First Name: "<<ll[c].firstName<<" Last Name: "<<ll[c].lastName;
cout<<" Member ID: "<<ll[c].memberID<<" Telephone Number: "<<ll[c].telephoneNo;
}// End of for loop
}// End of function
// Recursive function to search last name and returns the index position if found otherwise returns -1
// Using Binary Search
int searchLastName(lib ll[], int first, int last, string ln)
{
// Checks if last is greater than or equals to first
if (last >= first)
{
// Calculates the middle
int middle = first + (last - 1) / 2;
// Checks if the middle index position last name is equals to the last name passed as parameter then return the middle index position value
if (ll[middle].lastName.compare(ln) == 0)
return middle;
// Checks if the middle index position last name is greater than the last name passed as parameter
// then recursively calls the function by changing last to middle minus one
if (ll[middle].lastName.compare(ln) > 0)
return searchLastName(ll, first, middle - 1, ln);
// Otherwise, the middle index position last name is less than the last name passed as parameter
// then recursively calls the function by changing first to middle plus one
return searchLastName(ll, middle + 1, last, ln);
}// End of if condition
// We reach here when element is not found
return -1;
}// End of function
// Function to display record based on index position
void displayIndex(lib lib[], int pos)
{
cout<<" Record - "<<pos+1;
cout<<" First Name: "<<lib[pos].firstName<<" Last Name: "<<lib[pos].lastName;
cout<<" Member ID: "<<lib[pos].memberID<<" Telephone Number: "<<lib[pos].telephoneNo;
}// End of function
// Function to search member id, if found returns the index position otherwise, returns -1
// Using linear search
int searchID(lib ll[], int len, int id)
{
// Loops till length
for(int x = 0; x < len; x++)
{
// Checks if the current index position member id is equals to id passed as parameter
if(ll[x].memberID == id)
// Return x value as found index position
return x;
}// End of for loop
// We reach here when element is not found
return -1;
}// End of function
// Function to display menu and return the user choice
int menu()
{
int choice;
cout<<" 1 to search by last Name";
cout<<" 2 to search by id";
cout<<" 3 to Exit";
cout<<" Enter your choice: ";
cin>>choice;
return choice;
}// End of function
// main function definition
int main()
{
int id, pos;
string lastName;
library lib[MAX];
int len;
// Calls the function to read file contents
len = readFile(lib);
// Calls the function to display all the records
displayRecord(lib, len);
// Loops till user choice is not 3
do
{
// Calls the function to display menu and check based on user choice
switch(menu())
{
case 1:
cout<<" Enter the last name to search: ";
cin>>lastName;
pos = searchLastName(lib, 0, len-1, lastName);
if(pos == -1)
cout<<" Record not found.";
else
displayIndex(lib, pos);
break;
case 2:
cout<<" Enter the id to search: ";
cin>>id;
pos = searchID(lib, len, id);
if(pos == -1)
cout<<" Record not found.";
else
displayIndex(lib, pos);
break;
case 3:
exit(0);
default:
cout<<" Invalid Choice!";
}// End of switch case
}while(1);// End of do while loop
}// End of main function
Sample Output:
Record - 1
First Name: Anil Last Name: Acharya
Member ID: 119 Telephone Number: 5689234567
Record - 2
First Name: Punit Last Name: Das
Member ID: 117 Telephone Number: 8836547897
Record - 3
First Name: Amit Last Name: Kar
Member ID: 115 Telephone Number: 1896589891
Record - 4
First Name: Bhagaban Last Name: Muduli
Member ID: 116 Telephone Number: 1936777899
Record - 5
First Name: Ram Last Name: Padhy
Member ID: 111 Telephone Number: 1116445891
Record - 6
First Name: Ramesh Last Name: Panda
Member ID: 113 Telephone Number: 3336547771
Record - 7
First Name: Suressh Last Name: Patro
Member ID: 114 Telephone Number: 1886599891
Record - 8
First Name: Pyar Last Name: Sahu
Member ID: 111 Telephone Number: 1236547891
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 8
Invalid Choice!
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 1
Enter the last name to search: Kar
Record - 3
First Name: Amit Last Name: Kar
Member ID: 115 Telephone Number: 1896589891
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 1
Enter the last name to search: Sahu
Record - 8
First Name: Pyar Last Name: Sahu
Member ID: 111 Telephone Number: 1236547891
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 1
Enter the last name to search: Ram
Record not found.
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 2
Enter the id to search: 220
Record not found.
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 2
Enter the id to search: 117
Record - 2
First Name: Punit Last Name: Das
Member ID: 117 Telephone Number: 8836547897
1 to search by last Name
2 to search by id
3 to Exit
Enter your choice: 3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.