(Online Address Book Revisited) Programming Exercise 9 in Chapter 3 could handle
ID: 3741117 • Letter: #
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 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 <iostream>
#include <string>
#include <ctype.h>
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;
}
}
Explanation / Answer
// File Name: LinkedListAddress.cpp
#include <fstream>
#include <iostream>
#include <string.h>
#include <ctype.h>
using namespace std;
// Declares a constant for maximum length of the string
const int myMaxLength = 255;
// Creates a structure for address list
struct myList
{
// To store name
char myName[myMaxLength];
// To store address
char myAddress[myMaxLength];
// To store phone number
char myPhone[myMaxLength];
// To store pointer to next node
myList *myNext;
};// End of structure
// Renames myList to myAddress
typedef myList myAddress;
// Prototype of functions
void insertToList();
void removeFromList();
void displayName();
void listMyNames();
void freeMyList();
void writeList();
// Creates a head pointer and assigns it to null
myList *myHead = NULL;
// main function definition
int main()
{
// To store user choice
char myInput;
// Loops till user choice is not 'E'
while (1)
{
// Displays menu
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 ";
// Accepts user choice
cin >> myInput;
// Checks user choice and calls appropriate function
switch (toupper(myInput))
{
case 'A':
// Calls the function to insert a node
insertToList();
break;
case 'B':
// Calls the function to delete a node
removeFromList();
break;
case 'C':
// Calls the function to display a node information
displayName();
break;
case'D':
// Calls the function to display all names
listMyNames();
break;
case 'E':
// Calls the function to write list
writeList();
// Calls the function to delete all the nodes
freeMyList();
return (0);
default:
cout << "Unknown command ";
}// End of switch case
}// End of while loop
}// End of main function
// Function to insert a node to list
void insertToList()
{
// Creates a new node
myAddress *mynewName = new myAddress();
// Accepts data
cout << "Enter name ";
cin >> mynewName->myName;
cout << " Enter address ";
cin >> mynewName->myAddress;
cout << " Enter phone number ";
cin >> mynewName->myPhone;
// new node next points to head
mynewName->myNext = myHead;
// new node address is assigned to head
myHead = mynewName;
}// End of function
void removeFromList()
{
// Creates two pointers to point of type myAddress
myAddress *removePtr;
myAddress *previousPtr;
// To store the name entered by the user
char delName[myMaxLength];
cout << "Enter name to delete: ";
// Accepts the name
cin >> delName[myMaxLength];
// Checks if the head is null list is empty
if (myHead == NULL)
{
cout << "No list to delete ";
return;
}// End of if condition
// Checks if the current node name is equals to name entered by the user
if (strcmp(myHead->myName, delName) == 0)
{
// head address is assigned to remove pointer
removePtr = myHead;
// move to next node
myHead = myHead->myNext;
// delete the node pointed by remove pointer
delete removePtr;
return;
}// End of if condition
// head addressed is assigned to previous pointer
previousPtr = myHead;
// Loops till previous pointer next is not null
while (previousPtr->myNext != NULL)
{
// Checks if the current node name is equals to name entered by the user
if (strcmp(previousPtr->myNext->myName, delName) == 0)
{
// previous pointer next address is assigned to remove pointer
removePtr = previousPtr->myNext;
// Move to next node
previousPtr->myNext = removePtr->myNext;
// delete the node pointed by remove pointer
delete removePtr;
return;
}// End of if condition
// Previous pointer is pointing to previous pointer next
previousPtr = previousPtr->myNext;
}// End of while loop
// Otherwise not found
cout << " No Name found ";
}// End of function
// Function to display node contents
void displayName()
{
// To store the name entered by the user
char myName[myMaxLength];
// Creates a temporary pointer
myAddress *mysearchPtr;
// Accepts a name from the user
cout << "Enter the name you want to look for: ";
cin >> myName[myMaxLength];
// temporary pointer points to head
mysearchPtr = myHead;
// Loops till temporary pointer is not null
while (mysearchPtr != NULL)
{
// Checks if the current node name is equals to name entered by the user
if (strcmp(mysearchPtr->myName, myName) == 0)
{
// Displays the address and telephone number of the searched node
cout << "Address: " << mysearchPtr->myAddress << endl;
cout << "Telephone #: " << mysearchPtr->myPhone << endl;
return;
}// End of if condition
// Temporary pointer points to next node
mysearchPtr = mysearchPtr->myNext;
}// End of while loop
cout << "No name found! ";
}// End of function
// Function to display names stored in all nodes
void listMyNames()
{
// Declares a temporary pointer
myAddress *mytempPtr;
cout << "All names in address book: " << endl;
// temporary pointer points to head
mytempPtr = myHead;
// Loops till temporary pointer is not null
while (mytempPtr != NULL)
{
// Displays the name stored in the current node
cout << mytempPtr->myName << endl;
// Move to next node
mytempPtr = mytempPtr->myNext;
}// End of while loop
}// End of function
// Function to delete all nodes
void freeMyList()
{
// Declares a temporary pointer
myAddress *removePtr;
// Loops till head is not equals to null
while (myHead != NULL)
{
// Stores the address of the head in temporary node
removePtr = myHead;
// Move next
myHead = myHead->myNext;
// Delete the temporary node
delete removePtr;
}// End of while loop
}// End of function
// Function to write all the nodes to the file
void writeList()
{
// Declares ofstream object
ofstream outFile;
// Opens the file for writing
outFile.open("address.txt");
// Creates a temporary pointer points to head
myAddress *mysearchPtr = myHead;
// Loops till temporary node is not equals to null
while (mysearchPtr != NULL)
{
// Write the record to file
outFile<<mysearchPtr->myName<<" "<<mysearchPtr->myAddress<<" "<<mysearchPtr->myPhone<<endl;
// Move to next
mysearchPtr = mysearchPtr->myNext;
}// End of while loop
}// End of function
Sample Output:
Welcome to the Address book
Pick an option:
A. Add a person
B.Delete a person
C.Print
D.List names
E.Quit
a
Enter name Pyari
Enter address BAM
Enter phone number 1233215566
Welcome to the Address book
Pick an option:
A. Add a person
B.Delete a person
C.Print
D.List names
E.Quit
a
Enter name Mohan
Enter address BBSR
Enter phone number 1112323561
Welcome to the Address book
Pick an option:
A. Add a person
B.Delete a person
C.Print
D.List names
E.Quit
a
Enter name Sahu
Enter address RKL
Enter phone number 1255545654
Welcome to the Address book
Pick an option:
A. Add a person
B.Delete a person
C.Print
D.List names
E.Quit
d
All names in address book:
Sahu
Mohan
Pyari
Welcome to the Address book
Pick an option:
A. Add a person
B.Delete a person
C.Print
D.List names
E.Quit
e
address.txt file contents
Sahu
RKL
1255545654
Mohan
BBSR
1112323561
Pyari
BAM
1233215566
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.