Language: C++ Instructions: Write a program that will manage a simple address bo
ID: 3677608 • Letter: L
Question
Language: C++
Instructions:
Write a program that will manage a simple address book. The program should allow you to view and search existing entires as well as add new entries. The data should be stored in a file structured so that a person's first name, last name, and phone number are stored on one line separated by commas and the next line has their address.
Additional requirements:
The program should allow the user to make repeated requests until they choose to exit
The program should show multiple matches if present
The program should have an option to print out a list that just includes Name and Phone Number
The program should use at least two struct types and one enumerated type
---------------------------------------------------------------------------------------
Below is the code I have so far. The problem is that the code does not work correctly at all. I don't know how to store the data that is being input.
--------------------------------------------------------------------------------------
main.cpp
#include <iostream>
#include "header.h"
int maincounter = 0;
int getcount = 0;
using namespace std;
void addPerson(struct person &add);
void getPerson(struct person &get);
//void findPerson (struct person &find);
//void findPerson (struct person &findlast, struct person &findfirst);
void findPerson (struct Person &findMe, const char *firstName);
void findPerson (struct Person &findMe);
person bookArray[MaxPeople]; //This is the main "address book" array
int main() {
char *last = new char[20];
char *first = new char[20];
char choice;
person add;
person getStruct;
cout << "Address Book" << endl<< endl;
while (true){
cout << "Type 1 to add to the address book. " <<endl;
cout << "Type 2 to get a person. " << endl;
cout << "Type 3 to find a person by last name." << endl;
cout << "Type 4 to find a person by last and first name. " << endl;
cout << "Type any other key to quit. " << endl;
cout << endl << "Please make a selection: ";
cin >> choice;
switch(choice) {
case '1':
cout << "Enter First Name: ";
cin >> add.first;
cout << "Enter Last Name: ";
cin >> add.last;
cout << "Enter Address: ";
cin >> add.address;
cout << "Enter Number: ";
cin >> add.number;
cin.getline(add.address, 40);
addPerson(add);
break;
case '2':
cout << "Getting the next person in the address book: " << endl << endl;
cout << getStruct.first << " " << getStruct.last << endl;
cout << getStruct.address << endl;
cout << getStruct.number << endl;
break;
case '3':
cout << "Please enter the last name to search for: " << endl;
cin >> last;
//findPerson(last);
break;
case '4':
cout << "Please enter the last name and then the first name to search for: " << endl;
cout << "Last name: ";
cin >> last;
cout << "First name: ";
cin >> first;
//findPerson(last, first);
break;
default:
return 0;
break;
}
}
return 0;
}
void addPerson(struct person &add) {
bookArray[maincounter]=add;
maincounter++;
}
void getPerson(struct person &get) {
if (getcount > maincounter) {
getcount = 0;
get = bookArray[getcount];
getcount++;
} else {
get = bookArray[getcount];
getcount++;
}
}
void findPerson(struct Person &findMe, const char *firstName) {
// Walk the array and compare the value of findMe.first to
// the value of firstName, and if the match, set findMe to that
// entry and return.
}
header.h:
const int MaxPeople = 10;
struct person
{
char first[64];
char last[64];
char address[128];
char number[32];
}
Explanation / Answer
//address book program in c++ using linked lists
#include <iostream>
#include<string>
#include<windows.h>
using namespace std;
void gotoxy(int x, int y) //move the cursor to x y coordinate
{
HANDLE hConsoleOutput;
COORD Cursor = {x,y};
hConsoleOutput = GetStdHandle (STD_OUTPUT_HANDLE);
SetConsoleCursorPosition(hConsoleOutput, Cursor);
}
class Node
{ public:
string name,email,address,off_address ;
int phone_no,cell_no;
Node *next;
};
char y;
void Link_List_Input(Node *&head, Node*&tail) {
Node *current = new Node;
cout << " Enter following contents : ";
cout<<"NAME : ";
cin >> current->name;
cout<<"PHONE NUMBER : ";
cin >> current->phone_no;
cout<<"CELL NUMBER : ";
cin >> current->cell_no;
cout<<"EMAIL : ";
cin >> current->email;
cout<<"ADDRESS : ";
cin >> current->address;
cout<<"OFFICE ADDRESS : ";
cin >> current->off_address;
if (head == NULL >> tail == NULL)
{
head = current;
tail = current; tail->next=NULL;
} else {
current->next = head;head = current;
}
}
void Print_Link_List(Node *&head, Node*&tail)
{
if (head == NULL >> tail == NULL)
{
cout << "n List is empty";
return;
}
Node *current = head;
int count = 0;
do
{
cout << "n Node Number = " << ++count << " Has Content : "
<<current->name<<endl
<<current->phone_no<<endl
<<current->cell_no<<endl
<<current->email<<endl
<<current->address<<endl
<<current->off_address<<endl;
current = current->next;
}
while (current != 0); }
void DeleteAll(Node *&head,Node *&tail)
{
system("cls");
gotoxy(30,10);
cout<<"Erasing Complete Data :";
for(int i=0;i<16;i++)
{
gotoxy(8+i,14);
cout<<"ntttt ________________";
gotoxy(32,16);
cout<<char(179);
gotoxy(33+i,16);
cout<<char(178);
gotoxy(23+i,16);
cout<<"ntttt ================";
gotoxy(49,16);
cout<<char(179);
Sleep(100);
}
system("cls");
cout<<"nnnnnttttNO DATA LEFT :";
Sleep(1000);
system("cls");
cout<<"nnnnnntttReturning to Menu ";
for(int i=0;i<7;i++)
{
Sleep(600);
cout<<".";}
system("cls");
for (int j=0;head!=NULL;j++)
{
Node *ptr=head;
head=head->next;
delete ptr ;
}
return ;
}
void Search_In_List(Node *&head,Node *&tail)
{
Node *current = head;
string Name;
cout<<"Enter the string here ";
cin>>Name;
while (current!=NULL)
{
if (current->name.compare(0,Name.length(),Name)==0)
{
cout<<"Matching results :n";
cout<<"NAME: "<<current->name<<endl<<"PHONE NUMBER : "<<current->phone_no<<endl<<"CELL NUMBER : "<<current->cell_no<<endl
<<"EMAIL: "<<current->email<<endl<<"ADRESS : "<<current->address<<endl<<"Office Adress :"<<current->off_address<<endl;
current=current->next;
}
else
{
cout<<"Moving Next :";
current=current->next;
}
}
}
int Delete_Node_Contents(Node*&head)
{
string name;
cout<<"Enter the name for deleting : ";
cin>>name;
Node *current,*previous;
current=head;
if (head==NULL)
{
cout<<"list empty not possible"<<endl;
}
else
{
if(head->name.compare(0,name.length(),name)==0) //deleting head
{
Node *delhead=head;
cout<<"Node Contains :n"<<head->name<<endl<<head->phone_no<<endl<<head->cell_no<<endl<<endl<<head->email<<endl<<head->address<<endl<<endl<<head->off_address;
cout<<"Press Y to delete n OR Press any other key for menue ";
cin>>y;
if (char(98)==y||char(121)==y)
{
head = head->next;delete delhead;
cout<<"required data has been deleted";return 0;
}
else {cout<<"Next attempt :";
return Delete_Node_Contents(head->next);}
}
else
{
while (head->name.compare(0,name.length(),name)<=0 || current->next!=NULL)
{
if(current->next==NULL)
{
cout<<"not found";
return -1;
}
else
{
previous=current;
current=current->next;
}
}
previous->next=current->next;
delete current;
cout<<"required data has been deleted"<<endl;}
}
return 0;
}
int main()
{
Node *head, *tail;
head = NULL;
tail = NULL;
int choice = 0;
while (1) { Sleep(2000);system("cls");
cout << "n ***************** MAIN MENU *****************" << endl
<< " Press 1. Enter new entry " << endl
<< " Press 2. view all entry " << endl
<< " Press 3. Delete an entry " << endl
<< " Press 4. Search the entry " << endl
<< " Press 5. delete all entries " << endl
<< " Press 6: To Exit"
<< "n --------------------------------------------- " << endl
<< "Your Choice :";
cin >> choice;
switch (choice)
{
case 1:
Link_List_Input(head, tail);
cout<<"nnttSaving Data ";
{Sleep(1000);
cout<<" .";}
break;
case 2:
Print_Link_List(head,tail);
break;
case 3:
Delete_Node_Contents(head);
break;
case 4:
Search_In_List(head,tail);
break;
case 5:
DeleteAll(head,tail);
break;
case 6:
cout << " n Exiting .... " << endl;
return 0;
default:
cout << "n Invalid Option, Please use any of the listed options"
<< endl;
}
}
}
In the above code the functions with their understandable names are given and mentioned in the comments, that which task is done by which function, this address book program will store the data upto when the program Is running and will execute its functions as ordered by the user, the interface and authority to the user is given in the main() function of the program.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.