C Programming in structure and strings A library has a sorte
C++ Programming in structure and strings
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.
File name: library_database.txt
Nola Given 554097 205-588-2540
David Glowacki 672020 579-619-2775
Elbert Gorman 867054 896-273-7996
Sheryl Gregorio 406150 629-726-4934
Racquel Hamiton 415884 229-481-9483
Alease Hamlin 604458 890-994-8622
Loralee Hargrove 366958 585-386-9775
Ruthann Harvell 345462 523-975-0893
Eliseo Hendershott 844053 790-162-9692
Danyel Hinson 813408 787-389-4715
The program should also have a function that allows a librarian to retrieve a user’s information based on his/her last name or member ID. So, the program should have a menu-driven user interface. In case of the last name search all members that match should be displayed. If no members match, a message saying so should be displayed.
Solution
Library.CPP :
#include<iostream.h>
#include<fstream.h>
#include<conio.h>
#include<string.h>
void retrieve_data(char[]);
struct user{
char FirstName[20];
char LastName[20];
char memberID[20];
char TelephoneNumber[20];
}u[100];
int main(){
clrscr();
char uid[20];
ifstream infile;
infile.open(\"library_database.txt\");
for(int i=0;i<10;i++){
infile>>u[i].FirstName;
infile>>u[i].LastName;
infile>>u[i].memberID;
infile>>u[i].TelephoneNumber;
cout<<u[i].FirstName<<\" \"<<u[i].LastName<<\" \"<<u[i].memberID<<\" \"<<u[i].TelephoneNumber<<endl;
}
cout<<\"\ Enter User LastName or MemberID:\";
cin>>uid;
retrieve_data(uid);
infile.close();
getch();
return 0;
}
void retrieve_data(char uid[]){
int flag=0;
for(int i=0;i<100;i++){
if(!strcmp(uid,u[i].LastName)||!strcmp(uid,u[i].memberID)){
cout<<endl<<u[i].FirstName<<\" \"<<u[i].LastName<<\" \"<<u[i].memberID<<\" \"<<u[i].TelephoneNumber;
flag++;
}
}
if(flag==0){
cout<<\"No member should be displayed\"<<endl;
}
}
library_database.txt :
Nola Given 554097 2055882540
David Glowacki 672020 5796192775
Elbert Gorman 867054 8962737996
Sheryl Gregorio 406150 6297264934
Racquel Hamiton 415884 2294819483
Alease Hamlin 604458 8909948622
Loralee Hargrove 366958 5853869775
Ruthann Harvell 345462 5239750893
Eliseo Hendershott 844053 7901629692
Danyel Hamiton 813408 7873894715
Sample Input & Output :
Nola Given 554097 2055882540
David Glowacki 672020 5796192775
Elbert Gorman 867054 8962737996
Sheryl Gregorio 406150 6297264934
Racquel Hamiton 415884 2294819483
Alease Hamlin 604458 8909948622
Loralee Hargrove 366958 5853869775
Ruthann Harvell 345462 5239750893
Eliseo Hendershott 844053 7901629692
Danyel Hamiton 813408 7873894715
Enter User LastName or MemberID:Hamiton
Racquel Hamiton 415884 2294819483
Danyel Hamiton 813408 7873894715


