Question

Lab #2 Address Book Unsorted List (C++) Using classes, design an online address book to keep...

Lab #2 Address Book Unsorted List (C++)

Using classes, design an online address book to keep track of the names (first and last), addresses, phone numbers, and dates of birth.

The menu driven program should perform the following operations:

  • Load the data into the address book from a file
  • Write the data in the address book to a file
  • Search for a person by last name or phone number (one function to do both)
  • Add a new entry to the address book
  • Delete an entry from the address book based on a phone number or last name
  • input file has only unique records
  • Implement address book as unordered linked list
  • No error checking of the data in the input file needed
  • All records in the input file unique, no duplicates

Sample of a record

John Doe 6202 Winnetka Canoga ca 91336

Record format

  • one record per line, items separated by a blank space
  • First and last Names are one word each
  • building number: one word ( 6202)
  • street name : one word, no str. blvd. etc ( Victory)
  • city: one word ( Reseda )
  • phone: 10 digits, no formatting characters ( 8187196458)
  • birth date: 07 20 1965 ( month, day, year), no formatting characters

PLEASE do this in C++ and with multiple classes,like one for person data and one for address data, etc. My biggest problem though is with linked list. I can do pretty much the same thing with arrays but linked lists are very vague to me. It needs to be in classes and not structs.

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

#include<iostream> //for cin and cout
#include<fstream> //for ifstream and cfstream to work with files
using namespace std;
//record class consists of attributes of person and next pointer
class record

{
public:
string fname,lname,bnumber,stname,city,phone,bdate;
struct record *next;
};
//addressBook class has all the operations to be done on records
class addressBook

{
public:
record *root;
addressBook(){
root=NULL;
}
//loads Data from file and generates linked list
void loadData()

{
ifstream in;
in.open("input.txt");
if(in==NULL){
cout<<"error: unable to open input file";
return;
}
record *dummy=NULL;
root=new record;
dummy = root;
while(true)

{
record *p=NULL;
p=new record;
if(in>>p->fname>>p->lname>>p->bnumber>>p->stname>>p->city>>p->phone>>p->bdate)

{
dummy->next = p;
p->next=NULL;
dummy = p;
}else break;
}
root = root->next;
in.close();
cout<<"Data loaded!!";
}
//writes data from linked list to file
void writeData()

{
ofstream out;
out.open("input.txt");
if(out==NULL){
cout<<"error: unable to open file to write data";
return;
}
record *p = root;
while(p)

{
out<<p->fname<<' '<<p->lname<<' '<<p->bnumber<<' ';
out<<p->stname<<' '<<p->city<<' '<<p->phone<<' '<<p->bdate<<'\n';
p=p->next;
}
out.close();
cout<<"Data written into file successfully";
}
//search for the string in the linked list
void search(string s){
record *p = root;
while(p)

{
if(p->lname==s||p->phone==s)

{
cout<<"Name: "<<p->fname<<' '<<p->lname<<endl;
cout<<"Building number: "<<p->bnumber<<endl;
cout<<"Street name: "<<p->stname<<endl;
cout<<"City: "<<p->city<<endl;
cout<<"Phone number: "<<p->phone<<endl;
cout<<"Birth date: "<<p->bdate<<endl;
return;
}
p=p->next;
}
cout<<"\nNo record found";
}
//adds new record to linked list
void add()

{
record *p=NULL;
p=new record;
cout<<"Enter first name: ";
cin>>p->fname;
cout<<"Enter last name: ";
cin>>p->lname;
cout<<"Enter building number: ";
cin>>p->bnumber;
cout<<"Enter street name: ";
cin>>p->stname;
cout<<"Enter city: ";
cin>>p->city;
cout<<"Enter Phone number: ";
cin>>p->phone;
cout<<"Enter Birth date: ";
cin>>p->bdate;
p->next = root;
root=p;
cout<<"\nRecord added successfully";
}
//deletes record from linked list with lastname or phone number as s
void delet(string s)

{
record *p=NULL,*c=root;
while(c&&(c->lname==s||c->phone==s))

{
p=c;
c=c->next;
}
if(p&&c)

{
p=c->next;
cout<<"Record deleted successfully";
}

else if(p==NULL)

{
root=root->next;
cout<<"Record deleted successfully";
}
else cout<<"No record found. Deletion failed";
}
};
//menu displays the options to user.
void menu()
{
cout<<"\n1. Search for a person by last name or phone number\n";
cout<<"2. Add a new entry\n3. Delete an entry based on phone number or lastname\n";
cout<<"4. exit\n";
}

//main function implementation
int main()
{
addressBook ab; //created an object for class addressBook
ab.loadData(); //calls loadData function initially

int choice=0; //to store the user input
while(choice!=4){ //until user enters 4 the loop continues
menu();
cout<<"Enter choice: ";
cin>>choice;
if(choice==1)

{
cout<<"Enter last name or phone number: ";
string s;
cin>>s;
ab.search(s);
}
else if(choice==2)

{
ab.add();
}
else if(choice==3)

{
cout<<"Enter last name or phone number: ";
string s;
cin>>s;
ab.delet(s);
}
}
ab.writeData(); //calls writeData function
cout<<"\nBye!!";
return 0;
}

Add a comment
Know the answer?
Add Answer to:
Lab #2 Address Book Unsorted List (C++) Using classes, design an online address book to keep...
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
  • Write a program in C++: Using classes, design an online address book to keep track of the names f...

    Write a program in C++: Using classes, design an online address book to keep track of the names first and last, addresses, phone numbers, and dates of birth. The menu driven program should perform the following operations: Load the data into the address book from a file Write the data in the address book to a file Search for a person by last name or phone number (one function to do both) Add a new entry to the address book...

  • (JAVA) Using classes and inheritance, design an online address book to keep track of the names

    (JAVA)Using classes and inheritance, design an online address book to keep track of the names, addresses, phone numbers and birthdays of family members, friends and business associates. Your program should be able to handle a maximum of 500 entries.Define the following classes: Class Address to store a street name, city, state and zip code. Class Date to store the day, month and year. Class Person to store a person's last name and first name. Class ExtPerson that extends the class...

  • C++ program: Write an address book program that will accomplish the following: 1. Read name and...

    C++ program: Write an address book program that will accomplish the following: 1. Read name and address data from the user from the keyboard. 2. While reading the names and addresses, put the names into an appropriate data structure. 3. After reading names and addresses, allow user to search for names and change the names or addresses in the container data structure. 4. Allow user to write out the container data structure to a comma delimited file. The input file...

  • Requirements Create an Address Book class in Java for general use with the following behaviors: 1....

    Requirements Create an Address Book class in Java for general use with the following behaviors: 1. Constructor: public Address Book Construct a new address book object. • A contact has four fields: first name, last name, email and phone. (There could be more information for a real contact. But these are sufficient for the assignment.) . The constructor reads from the disk to retrieve previously entered contacts. If previous contacts exist, the address book will be populated with those contacts...

  • language:python VELYIEW Design a program that you can use to keep information on your family or...

    language:python VELYIEW Design a program that you can use to keep information on your family or friends. You may view the video demonstration of the program located in this module to see how the program should function. Instructions Your program should have the following classes: Base Class - Contact (First Name, Last Name, and Phone Number) Sub Class - Family (First Name, Last Name, Phone Number, and Relationship le. cousin, uncle, aunt, etc.) Sub Class - Friends (First Name, Last...

  • 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...

  • (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure...

    (In C) 8.12 LAB: Warm up: Contacts You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. ContactNode.h - Struct definition, including the data members and related function declarations ContactNode.c - Related function definitions main.c - main() function (2) Build the ContactNode struct per the following specifications: Data members char contactName[50] char contactPhoneNum[50] struct ContactNode* nextNodePtr Related functions CreateContactNode() (2 pt) InsertContactAfter() (2 pts) Insert...

  • C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header ...

    C++ project we need to create a class for Book and Warehouse using Book.h and Warehouse.h header files given. Then make a main program using Book and Warehouse to read data from book.dat and have functions to list and find book by isbn Objectives: Class Association and operator overloading This project is a continuation from Project 1. The program should accept the same input data file and support the same list and find operations. You will change the implementation of...

  • C++ In this assignment, you will write a class that implements a contact book entry. For...

    C++ In this assignment, you will write a class that implements a contact book entry. For example, my iPhone (and pretty much any smartphone) has a contacts list app that allows you to store information about your friends, colleagues, and businesses. In later assignments, you will have to implement this type of app (as a command line program, of course.) For now, you will just have to implement the building blocks for this app, namely, the Contact class. Your Contact...

  • Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list -...

    Please use C++ CS3358 Insert and delete a node Programming Project 2: The linked list - Reference: chapter 18: Create an array of 15 student records that should not be sorted Create a liked list of 15 student record nodes. Each node is a node of one student record from the above unsorted array. The list of student records should be sorted by student ID. (Insert function without sort function to create a linked list.) (If you insert correctly, the...

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