Question

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
  • 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 un-ordered 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

Steve Brown 4592 Raleigh Seathle WA 98001

Record format

  • one record per line, items separated by a blank space
  • First and last Names are one word each
  • building number: one word (4592)
  • street name : one word, no str. blvd. etc (Raleigh)
  • city: one word (Seatle)
  • phone: 10 digits, no formatting characters (2065551987)
  • birth date: 05 10 2001 ( month, day, year), no formatting characters
0 0
Add a comment Improve this question Transcribed image text
Answer #1

CODE:

#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;
}

//input.txt before execution

input Notepad File Edit Format View Help John Doe 8798 walnutstreet california 8787329982 03121987 Steven Fincher 4932 Victor

//input.txt after execution

File Edit Format View Help James Winger 2412 Rage Washington 9483984933 02121984 Steven Fincher 4932 Victory Reseda 818612343

//output screenshots

CAC&C++cplus\bin\Debuglcplus.exe Data 1oaded!! 1. Search for a person by last name or phone number 2. Add a new entry 3. Dele

Enter choice: 4 Bye!! Process returned e (x execution time65.751 s Press any key to continue

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

  • In this assignment, you will write one (1) medium size C program. The program needs to...

    In this assignment, you will write one (1) medium size C program. The program needs to be structured using multiple functions, i.e., you are required to organize your code into distinct logical units. The following set of instructions provide the specific requirements for the program. Make sure to test thoroughly before submitting. Write   a   program,   named   program1.c,   that   reads   and   processes   employee   records   (data   about   an   employee).   Each   employee   record   must   be   stored   using   a   struct   that   contains   the   following  ...

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

  • Write a C program to compute average grades for a course. The course records are in...

    Write a C program to compute average grades for a course. The course records are in a single file and are organized according to the following format: Each line contains a student’s first name, then one space, then the student’s last name, then one space, then some number of quiz scores that, if they exist, are separated by one space. Each student will have zero to ten scores, and each score is an integer not greater than 100. Your program...

  • Write a java project that reads a sequence of up to 25 pairs of names and...

    Write a java project that reads a sequence of up to 25 pairs of names and postal (ZIP) codes, street address, city, state, and 10-digit phone number for individuals. Store the data in an object designed to store a first name (string), last name (string), and postal code (integer), street address (string), city( string), state (string), and 10-digit phone number (long integer, contains area code and does not include special characters such as '(', ')', or '-' . Assume each...

  • In C++, Write a program that retrieves a phone number from a txt files of names...

    In C++, Write a program that retrieves a phone number from a txt files of names and phone numbers. The program should ask the user to enter a name and then print the phone number of that person (or vice versa, number into name) The program should allow the user to repeat the operation as often as they would like to. The data is contained in two text files named "phoneNames.txt" and "phoneNums.txt". The files have one name or one...

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

  • Write this in a C program please. Structures on Disk The Problem Your task is to...

    Write this in a C program please. Structures on Disk The Problem Your task is to write a program that stores and retrieves structures in a file on disk. The file of structures must remain on the disk once your program ends. You should be able to store structures to the file and retrieve from the file after restarting the program. The record that you will be writing to file has the following structure: struct contact {unsigned long phone_number; long...

  • Write a complete C++ program that reads students names and their test scores from an input...

    Write a complete C++ program that reads students names and their test scores from an input text file. The program should output each student’s name followed by the test scores and the relevant grade in an output text file. It should also find and display on screen the highest/lowest test score and the name of the students having the highest/lowest test score, average and variance of all test scores. Student data obtained from the input text file should be stored...

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