This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a
ID: 3804415 • Letter: T
Question
This assignment was locked Mar 24 at 11:59pm.
For this lab you will implement a phone book using a linked list to keep people's names and phone numbers organized in alphabetical order.
1. Define a structure to hold the contact information including: name, phone number, a pointer to the next node in the list. Example:
struct ContactNode
{
string name;
string phoneNumber;
ContactNode *next;
}
2. Define a class containing the structure and the appropriate methods to update and retrieve contacts from the list. You will need a method to insert a node at the right place in order to keep the list sorted, a method to remove a contact from the list, a method to traverse the list to print all contacts, and also constructor and destructor. You can find an implementation for those operations in the book and slides.
3. insertContact() and deleteContact() should not have any input/output statements. All the interaction with the user will be in your main.cpp. For example, the method that removes a contact from the list should not ask the user for the contact's name. This should be done prior to calling the method that removes it.
4. Implement a main.cpp to test your class. You do not have to turn it in.
Turn in contact.cpp
PreviousNext
this is the header file they provided
/*
* Contact.h
*
* Created on: Mar 7, 2017
* Author: hellenpacheco
*/
#ifndef CONTACT_H_
#define CONTACT_H_
#include <string>
class Contact {
private:
struct ContactNode
{
std::string name;
std::string phoneNumber;
ContactNode *next;
};
ContactNode *head;
public:
Contact() { head = nullptr; }
virtual ~Contact(); // destroys the list one node at a time
void insertContact(std::string name, std::string phoneNumber); // do not allow duplicate names
void deleteContact(std::string name); // deletes the corresponding node from the list
void printContacts(); // prints all contacts on the screen
};
#endif /* CONTACT_H_ */
Explanation / Answer
#include <iostream.h>
#include <fstream.h>
#include <string.h>
#include <iomanip.h>
#include<conio.h>
class phoneBook
{
charname[20],phno[15];
public: void getdata();
voidshowdata();
char *getname()
{ return name; }
char*getphno()
{ return phno; }
void update(char *nm,char*telno)
{
strcpy(name,nm);
strcpy(phno,telno); } };
voidphoneBook :: getdata()
{ cout<<" Enter Name : ";
cin>>name;
cout<<"Enter Phone No. : ";
cin>>phno; }
void phoneBook :: showdata()
{
cout<<" "; cout<<setw(20)<<name;
cout<<setw(15)<<phno; }
voidmain()
{ phoneBook rec;
fstream file;
file.open("d:\phone.dat", ios::ate | ios::in | ios::out |ios::binary);
char ch,nm[20],telno[6];
int choice,found=0;
while(1)
{ clrscr();
cout<<" *****Phone Book***** "; cout<<"1)
Add New Record "; cout<<"2)
Display All Records "; cout<<"3) Search Telephone No. "; cout<<"4) Search Person Name "; cout<<"5) Update Telephone No. "; cout<<"6) Exit "; cout<<"Choose your choice : ";
cin>>choice;
switch(choice)
{ case 1 : //New Record rec.getdata();
cin.get(ch);
file.write((char *) &rec,sizeof(rec));
break;
case 2 : //Display All Recordsfile.seekg(0,ios::beg); cout<<" Records in Phone Book ";
while(file)
{ file.read((char *) &rec, sizeof(rec));
if(!file.eof()) rec.showdata(); }
file.clear();
getch();
break;
case 3 : //Search Tel. no. when person name is known.cout<<" Enter Name : ";
cin>>nm;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec))) { if(strcmp(nm,rec.getname())==0)
{ found=1;
rec.showdata(); } }
file.clear();
if(found==0) cout<<" ---Record Not found--- "; getch();
break;
case4 : //Search name on basis of tel. no
cout<<" Enter Telephone No : ";
cin>>telno;
file.seekg(0,ios::beg);
found=0;
while(file.read((char *) &rec, sizeof(rec))) {if(strcmp(telno,rec.getphno())==0)
{ found=1;
rec.showdata(); } }
file.clear();
if(found==0)
cout<<" ---Record Not found--- ";
getch();
break;
case 5 : //Update Telephone No. cout<<" Enter Name : "; cin>>nm;
file.seekg(0,ios::beg); found=0;
int cnt=0;
while(file.read((char *) &rec, sizeof(rec))) { cnt++;
if(strcmp(nm,rec.getname())==0)
{ found=1;
break; } }
file.clear();
if(found==0)
cout<<" ---Record Not found--- ";
else { int location = (cnt-1) * sizeof(rec); cin.get(ch);
if(file.eof()) file.clear();
cout<<"Enter New Telephone No : ";
cin>>telno;
file.seekp(location);
rec.update(nm,telno);
file.write((char *) &rec,sizeof(rec));
file.flush(); }
break;
case 6 : gotoout; } }
out: file.close(); }
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.