Question

This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone...

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_ */

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#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<<"\nEnter Name : ";

cin>>name;

cout<<"Enter Phone No. : ";

cin>>phno; }

void phoneBook :: showdata()

{

cout<<"\n"; 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<<"\n*****Phone Book*****\n"; cout<<"1)

Add New Record\n"; cout<<"2)

Display All Records\n"; cout<<"3) Search Telephone No.\n"; cout<<"4) Search Person Name\n"; cout<<"5) Update Telephone No.\n"; cout<<"6) Exit\n"; 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<<"\n\nRecords in Phone Book\n";

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<<"\n\nEnter 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<<"\n\n---Record Not found---\n"; getch();

break;

case4 : //Search name on basis of tel. no

cout<<"\n\nEnter 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<<"\n\n---Record Not found---\n";

getch();

break;

case 5 : //Update Telephone No. cout<<"\n\nEnter 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<<"\n\n---Record Not found---\n";

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(); }

Add a comment
Know the answer?
Add Answer to:
This assignment was locked Mar 24 at 11:59pm. For this lab you will implement a phone...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot...

    USING THIS CODE: #include <iostream> #include <string> using namespace std; class Contact { //this class cannot be viewed from outside of the private class. //only the class functions within the class can access private members. private: string name; string email; string phone; //the public class is accessible from anywhere outside of the class //however can only be within the program. public: string getName() const { return name; } void setName(string name) { this->name = name; } string getEmail() const {...

  • Write in C++ please: Write a class named MyVector using the following UML diagram and class...

    Write in C++ please: Write a class named MyVector using the following UML diagram and class attribute descriptions. MyVector will use a linked listed instead of an array. Each Contact is a struct that will store a name and a phone number. Demonstrate your class in a program for storing Contacts. The program should present the user with a menu for adding, removing, and retrieving Contact info. MyVector Contact name: string phone: string next: Contact -head: Contact -tail: Contact -list_size:...

  • a. Define the struct node that can store a name and up to 3 phone numbers...

    a. Define the struct node that can store a name and up to 3 phone numbers (use an array for the phone numbers). The node struct will also store the address to the next node. b. Define a class contactList that will define a variable to keep track of the number of contacts in the list, a pointer to the first and last node of the list. c. Add the following methods to the class: i. Add a new contact....

  • Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode ha...

    Declare and define TtuStudentNode in TtuStudentNode.h and TtuStudentNode.cpp file. TtuStudentNode has its unique string variable "studentEmail" which contains email address. TtuStudentNode has its unique function member GetEmail() which returns the string variable "studentEmail". TtuStudentNode has its overriding "PrintContactNode()" which has the following definition. void TtuStudentNode::PrintContactNode() { cout << "Name: " << this->contactName << endl; cout << "Phone number: " << this->contactPhoneNum << endl; cout << "Email : " << this->studentEmail << endl; return; } ContactNode.h needs to have the function...

  • //header files #ifndef Contact_H // header files should always have this to avoid #define Contact_H   ...

    //header files #ifndef Contact_H // header files should always have this to avoid #define Contact_H    // multiple inclusion in other files #include <string> // this is the only programming assignment which will use this statement. // normally "using namespace std" is looked down upon because it // introduces many common keywords that could be accidentally used, but // it identifies useful types such as string and would normally be used // std::string or std::vector. using namespace std; class Contact...

  • DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to...

    DESCRIPTION Create a C++ program to manage phone contacts. The program will allow the user to add new phone contacts, display a list of all contacts, search for a specific contact by name, delete a specific contact. The program should provide the user with a console or command line choice menu about possible actions that they can perform. The choices should be the following: 1. Display list of all contacts. 2. Add a new contact. 3. Search for a contact...

  • Hello I need some help with my CC+ project. My current project erasing the first two...

    Hello I need some help with my CC+ project. My current project erasing the first two phone numbers and only printing the 3rd phone number out in the list. Any idea on how to correct. Here is the error. Contacts.h #ifndef Contact_H #define Contact_H #include<string> using namespace std; class ContactNode{ public: ContactNode(); ContactNode(string name, string phone); void InsertAfter(ContactNode*); string GetName(); string GetPhoneNumber(); ContactNode* GetNext(); void PrintContactNode(); private: string contactName; string contactPhoneNum; ContactNode* nextNodePtr; }; #endif Contacts.cpp #include <iostream> #include "Contacts.h"...

  • Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names...

    Please help!! (C++ PROGRAM) You will design an online contact list to keep track of names and phone numbers. ·         a. Define a class contactList that can store a name and up to 3 phone numbers (use an array for the phone numbers). Use constructors to automatically initialize the member variables. b.Add the following operations to your program: i. Add a new contact. Ask the user to enter the name and up to 3 phone numbers. ii. Delete a contact...

  • Am Specification For this assignment, you will write a multi-file C program to define, implement ...

    Must be written and C, and compile with MinGW. Thank you! am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT