Print Friendly and PDF
#include <iostream>
using namespace std;
struct Node
{
char name;
int number;
char contactNo;
char email;
int count;
struct Node* next;
};
class ContatBook
{
Node* first;
Node* current;
Node* newNode;
public:
ContatBook()
{
first=0;
current=0;
newNode=0;
}
void clearInput(void);
void addNewcontact(void)
{
newNode = (struct Node*)malloc(sizeof(struct Node));
if(first==NULL)
first = current = newNode;
else
{
current = first;
while(current->next != NULL)current = current->next;
current->next = newNode;
current = newNode;
}
cout<<"Enter contact name"<<endl;
current->name;
cout<<"Enter contact Phone number"<<endl;
current->contactNo;
cout<<"Enter contact email"<<endl;
current->email;
cout<<"contact added!"<<endl;
current->count++;
current->next = NULL;
}
void listAll(void)
{
if(first==NULL)
puts("There are no contacts to display!");
else
{
cout<<"Acct No. is:"<<endl;
cout<<"Name is:"<<endl;
cout<<"Contact No. is:"<<endl;
cout<<"Email is:"<<endl;
current=first;
do {
cout<<"\n"<<endl;
current->number;
current->name;
current->contactNo;
current->email;
}
while((current=current->next) != NULL);
}
}
void deletecontact(void)
{
int record;
struct Node *previous;
if(first==NULL)
{
cout<<"There are no contacts to delete!"<<endl;
return;
}
listAll();
cout<<"Enter contact account number to delete: "<<endl;
cin>>record;
current = first;
while(current != NULL)
{
if(current->number == record)
{ if(current == first)
first=current->next;
else
previous->next = current->next;
free(current);  cout<<"contact %d deleted!\n"<<record<<endl;
return;
}
else
{
previous = current;
current = current->next;
}
}
cout<<"contact %d not found!\n"<<record<<endl;
}
void modifycontact(void)
{
int record, result;
if(first==NULL)
{
cout<<"There are no contacts to modify!"<<endl;
return;
}
listAll();
cout<<"Enter contact account number to modify or change: "<<endl; cin>>record;
result = findnum(record);
if( result >0 )
{
cout<<"Contact:"<<current->number<<endl;
cout<<"Name:"<<current->name<<endl;
if(prompt())
current->name;
cout<<"Contact No:"<<current->contactNo<<endl;
if(prompt())
current->contactNo;
cout<<"Email:"<<current->email<<endl;
if(prompt())
current->email;
return;
}
cout<<"contact %d was not found!\n"<<record<<endl;
}
int findcontact(void)
{
char buff[20];
if(first==NULL)
{
cout<<"There are no contacts to find!"<<endl;
return 1;
}
cout<<"Enter contact name: "<<endl;
fflush(stdin); current = first;
while(current != NULL)
{
if((current->name, buff) == 0 )
{
cout<<"Acct#"<<endl;
cout<<"Name"<<endl;
cout<<"Phone"<<endl;
cout<<"Email"<<endl;
cout<<"\n"<<endl;
current->number;
current->name;
current->contactNo;
current->email;
return 0;
}
else
{
current = current->next;
}
}
cout<<"contact %s was not found!\n"<<buff;
return 1;
}
int prompt(void)
{
char ch;
fflush(stdin);
cout<<"Update? (Y to update any other key to not)"<<endl;
ch = getchar();
ch = toupper(ch);
fflush(stdin);
if(ch == 'Y')
{
cout<<"Enter new value: "<<endl;
return(1);
}
else
return(0);
}
int findnum (int recordnum)
{
int record;
record = recordnum;
current = first;
while(current != NULL)
{
if(current->number == record)
{
return 1;
}
else
{ current = current->next;
}
}
return -1;
}
void display()
{
Node *temp=first;
while(temp != NULL)
{
cout<<temp->contactNo<<endl;
temp=temp->next;
}
}
};
void main()
{
//Node N;
ContatBook C;
int choice;
do
{
cout<<"\nWelcome To The Contact Book"<<endl;
cout<<"-- -----------------------------"<<endl;
cout<<"1 - Add a new contact"<<endl;
cout<<"2 - Delete contact"<<endl;
cout<<"3 - List all contacts"<<endl;
cout<<"4 - Modify contact"<<endl;
cout<<"5 - Find a contact by name"<<endl;
cout<<"-- -----------------------------"<<endl;
cout<<"Q - Save and quit\n"<<endl;
cout<<"\tYour choice:"<<endl;
cin>>choice;
choice = getchar();
choice = toupper(choice);
switch(choice)
{
case '1':
{
cout<<"Add a new contact\n"<<endl;
C.addNewcontact();
break;
}
case '2':
{
cout<<"Delete a contact\n"<<endl;
C.deletecontact();
break;
}
case '3':
{
cout<<"List all contacts\n"<<endl;
C.listAll();
break;
}
case '4':
{
cout<<"Modify a contact\n"<<endl;
C.modifycontact();
break;
}
case '5':
{
cout<<"Find a contact by name\n"<<endl;
C.findcontact();
break;
}
case 'Q':
{
cout<<"Save and quit\n"<<endl;
default:
break;
}
}
}while(choice != 'Q');
}
zubairsaif

Zubair saif

A passionate writer who loves to write on new technology and programming

Post A Comment:

0 comments: