Question: I have this contact manager program and am having issues changing it to use an array class, then the same code using a vector class.
I have this contact manager program and am having issues changing it to use an array class, then the same code using a vector class. Here is what I have.
#include
#include
#include
#include
using namespace std;
class contact
{ //Declare all variables long ph;
char name[20],add[20],email[30];
// Create a contact module to add information about the individual. public:
void create_contact()
{
cout<<"Phone: ";
cin>>ph;
cout<<"Name: ";
cin.ignore();
cin>>name;
cout<<"Address: ";
cin.ignore();
cin>>add;
cout<<"Email address: ";
cin.ignore();
cin>>email;
cout<<" ";
}
// DIsplay customer information void show_contact()
{
cout< cout< cout< cout< } long getPhone() { return ph; } char* getName() { return name; } char* getAddress() { return add; } char* getEmail() { return email; } }; fstream fp; contact cont; //Save customer information to the array void save_contact() { fp.open("contactBook.dat",ios::out|ios::app); cont.create_contact(); fp.write((char*)&cont,sizeof(contact)); fp.close(); cout< getchar(); } void show_all_contacts() { system("cls"); cout<<" \t\t-------------------------------- \t\t\tLIST OF CUSTOMERS \t\t-------------------------------- "; fp.open("contactBook.dat",ios::in); while(fp.read((char*)&cont,sizeof(contact))) { cont.show_contact(); cout< } fp.close(); } void display_contact(int num) { bool found; int ch; found=false; fp.open("contactBook.dat",ios::in); while(fp.read((char*)&cont,sizeof(contact))) { if(cont.getPhone()==num) { system("cls"); cont.show_contact(); found=true; } } fp.close(); if(found == false) { cout<<" No results found. Please try again.";} getchar(); } //Edit the customer information to update/correct information void edit_contact() { int num; bool found=false; system("cls"); cout<<"Edit customer ------------------------------- \tEnter the number of the customer that you wish to change:"; cin>>num; fp.open("contactBook.dat",ios::in|ios::out); while(fp.read((char*)&cont,sizeof(contact)) && found==false) { if(cont.getPhone()==num) { cont.show_contact(); cout<<" Please enter the new customer information: "< cont.create_contact(); int pos=-1*sizeof(cont); fp.seekp(pos,ios::cur); fp.write((char*)&cont,sizeof(cont)); cout< found=true; } } fp.close(); if(found==false) cout< } //Delete a customer from the array void delete_contact() { int num; system("cls"); cout< cin>>num; fp.open("contactBook.dat",ios::in|ios::out); fstream fp2; fp2.open("Temp.dat",ios::out); fp.seekg(0,ios::beg); while(fp.read((char*)&cont,sizeof(contact))) { if(cont.getPhone()!=num) { fp2.write((char*)&cont,sizeof(contact)); } } fp2.close(); fp.close(); remove("contactBook.dat"); rename("Temp.dat","contactBook.dat"); cout< } //Main module for the menu int main(int argc, char *argv[]) { cout<<"\t\t\t\t-\t-"; cout<<"\t\t\t\t--\t--"; cout<<"\t\t\t\t---\t---"; cout<<"\t\t\t\t----\t----"; cout<<"\t\t\t\t-----\t-----"; cout<<"\t\t\t\t------\t------"; cout<<"\t\t\t\t-------\t-------"; cout<<"\t\t\t\t------\t-------"; cout<<"\t\t\t\t------\t------"; cout<<"\t\t\t\t-----\t-----"; cout<<"\t\t\t\t----\t----"; cout<<"\t\t\t\t---\t---"; cout<<"\t\t\t\t--\t--"; cout<<"\t\t\t\t-\t-"; for(;;) { int ch; cout<<" \t ---- Welcome to the Contact Management System ----"; cout<<" \t\t\tMAIN MENU \t\t---------------------- \t\t[1] Add a new customer \t\t[2] List all customers \t\t[3] Search for a customer \t\t[4] Edit a customer \t\t[5] Delete a customer \t\t[0] Exit \t\t------------------ \t\t"; cout<<"Enter your choice:"; cin>>ch; switch(ch) { case 0: cout<<" \t\tHave a great day!"; exit(0); break; break; break; case 1:save_contact(); break; case 2:show_all_contacts(); break; case 3: int num; system("cls"); cout<<" \tPhone: "; cin>>num; display_contact(num); break; case 4:edit_contact(); break; case 5:delete_contact(); break; default: break; } int opt; cout<<" Enter your choice: \t[1] Main Menu\t\t[0] Exit "; cin>>opt; switch (opt) { case 1: system("cls"); continue; case 0: exit(0); } } return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
