Question
(Online Address Book Revisited) Programming Exercise 9 in Chapter 3 could handle a maximum of only 500 entries. Using linked lists, redo the program to
(Online Address Book Revisited)
Programming Exercise 9 in Chapter 3 could handle a maximum of only 500 entries.
Using linked lists, redo the program to handle as many entries as required.
Add the following operations to your program:
a. Add or delete a new entry to the address book.
b. When the program terminates, write the data in the address book to a disk.
This is the code I have so far, it runs but there are errors with how it runs.
// Ch5Ex1.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include
#include
#include
using namespace std;
const int myMaxLength = 255;
struct myList
{
char myName[myMaxLength];
char myAddress[myMaxLength];
char myPhone[myMaxLength];
myList *myNext;
};
typedef myList myAddress;
void insertToList();
void removeFromList();
void displayName();
void listMyNames();
void freeMyList();
myList *myHead = NULL;
int main()
{
char myInput;
while (1)
{
cout << "Welcome to the Address book ";
cout << "Pick an option: ";
cout << "A. Add a person B.Delete a person C.Print D.List names E.Quit ";
cin >> myInput;
switch (toupper(myInput))
{
case 'A':
insertToList();
break;
case 'B':
removeFromList();
break;
case 'C':
displayName();
break;
case'D':
listMyNames();
break;
case 'E':
freeMyList();
return (0);
default:
cout << "Unknown command ";
}
}
}
void insertToList()
{
myAddress *mynewName = new myAddress;
cout << "Enter name ";
cin >> mynewName->myName;
cout << " Enter address ";
cin >> mynewName->myAddress;
cout << " Enter phone number ";
cin >> mynewName->myPhone;
mynewName->myNext = myHead;
myHead = mynewName;
}
void removeFromList()
{
myAddress *removePtr;
myAddress *previousPtr;
char delName[myMaxLength];
cout << "Enter name to delete: ";
cin >> delName[myMaxLength];
if (myHead == NULL)
{
cout << "No list to delete ";
return;
}
if (strcmp(myHead->myName, delName) == 0)
{
removePtr = myHead;
myHead = myHead->myNext;
delete removePtr;
return;
}
previousPtr = myHead;
while (previousPtr->myNext != NULL)
{
if (strcmp(previousPtr->myNext->myName, delName) == 0)
{
removePtr = previousPtr->myNext;
previousPtr->myNext = removePtr->myNext;
delete removePtr;
return;
}
previousPtr = previousPtr->myNext;
}
cout << " No Name found ";
}
void displayName()
{
char myName[myMaxLength];
myAddress *mysearchPtr;
cout << "Enter the name you want to look for: ";
cin >> myName[myMaxLength];
mysearchPtr = myHead;
while (mysearchPtr != NULL)
{
if (strcmp(mysearchPtr->myName, myName) == 0)
{
cout << "Address: " << mysearchPtr->myAddress << endl;
cout << "Telephone #: " << mysearchPtr->myPhone << endl;
return;
}
mysearchPtr = mysearchPtr->myNext;
}
cout << "No name found! ";
}
void listMyNames()
{
myAddress *mytempPtr;
cout << "All names in address book: " << endl;
mytempPtr = myHead;
while (mytempPtr != NULL)
{
cout << mytempPtr->myName << endl;
mytempPtr = mytempPtr->myNext;
}
}
void freeMyList()
{
myAddress *removePtr;
while (myHead != NULL)
{
removePtr = myHead;
myHead = myHead->myNext;
delete removePtr;
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started