Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have this contact manager program and am having issues changing it to use an a

ID: 3920122 • Letter: I

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. Here is what I have.

#include<fstream>

#include<string>

#include<iomanip>

#include<iostream>

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<<endl<<"Phone number: "<<ph;

cout<<endl<<"Name: "<<name;

cout<<endl<<"Address: "<<add;

cout<<endl<<"Email Address : "<<email;

}

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<<endl<<endl<<"Customer has been added!";

getchar();

}

void show_all_contacts()

{

system("cls");

cout<<" -------------------------------- LIST OF CUSTOMERS -------------------------------- ";

fp.open("contactBook.dat",ios::in);

while(fp.read((char*)&cont,sizeof(contact)))

{

cont.show_contact();

cout<<endl<<"----------------------------------------------- "<<endl;

}

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 ------------------------------- Enter 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: "<<endl;

cont.create_contact();

int pos=-1*sizeof(cont);

fp.seekp(pos,ios::cur);

fp.write((char*)&cont,sizeof(cont));

cout<<endl<<endl<<" Customer has been changed!";

found=true;

}

}

fp.close();

if(found==false)

cout<<endl<<endl<<"Customer not found. Please try again.";

}

//Delete a customer from the array

void delete_contact()

{

int num;

system("cls");

cout<<endl<<endl<<"Please enter the customers number: ";

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<<endl<<endl<<" Customer Deleted.";

}

//Main module for the menu

int main(int argc, char *argv[])
{

cout<<" - -";

cout<<" -- --";

cout<<" --- ---";

cout<<" ---- ----";

cout<<" ----- -----";

cout<<" ------ ------";

cout<<" ------- -------";

cout<<" ------ -------";

cout<<" ------ ------";

cout<<" ----- -----";

cout<<" ---- ----";

cout<<" --- ---";

cout<<" -- --";

cout<<" - -";

for(;;)


{

int ch;

cout<<" ---- Welcome to the Contact Management System ----";

cout<<" MAIN MENU ---------------------- [1] Add a new customer [2] List all customers [3] Search for a customer [4] Edit a customer [5] Delete a customer [0] Exit ------------------ ";

cout<<"Enter your choice:";

cin>>ch;

switch(ch)

{

case 0: cout<<" Have 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<<" Phone: ";

cin>>num;

display_contact(num);

break;

case 4:edit_contact();

break;

case 5:delete_contact();

break;

default:

break;

}
  
int opt;

cout<<" Enter your choice: [1] Main Menu [0] Exit ";

cin>>opt;

switch (opt)

{

case 1:

system("cls");

continue;

case 0:

exit(0);

}

}
return 0;

}

Explanation / Answer

For Array


/*For array */

#include<fstream>
#include<string>
#include<iomanip>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>


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<<endl<<"Phone number: "<<ph;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Address: "<<add;
cout<<endl<<"Email Address : "<<email;
}
long getPhone()
{
return ph;
}

void operator = (contact a){
   ph = a.ph;
   strcpy(name,a.name);
   strcpy(add, a.add);
   strcpy(email,a.email);
}
char* getName()
{
return name;
}
char* getAddress()
{
return add;
}
char* getEmail()
{
return email;
}
};
fstream fp;
//contact cont;
//Save customer information to the array
void write_contacts(contact *data, int n)
{
fp.open("contactBook.dat",ios::out);
//cont.create_contact();
for (int i = 0; i<n; i++){
   fp.write((char*)&data[i],sizeof(contact));
}
fp.close();
cout<<endl<<endl<<"Customers have been saved to file!";
//getchar();
}

void show_all_contacts(contact *data, int n)
{
cout<<" -------------------------------- LIST OF CUSTOMERS -------------------------------- ";
   for (int i = 0; i<n; i++){
      data[i].show_contact();
      cout<<endl<<"----------------------------------------------- "<<endl;
   }
}
/*
void show_all_contacts()
{
//system("clear");
cout<<" -------------------------------- LIST OF CUSTOMERS -------------------------------- ";
fp.open("contactBook.dat",ios::in);
while(fp.read((char*)&cont,sizeof(contact)))
{
cont.show_contact();
cout<<endl<<"----------------------------------------------- "<<endl;
}
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("clear");
cont.show_contact();
found=true;
}
}
fp.close();
if(found == false)
{
cout<<" No results found. Please try again.";}
getchar();
}

*/

void display_contact(contact *data, int x, int n){

  
   for (int i = 0; i<n; i++){
       if (data[i].getPhone() == x){
          system("clear");
          data[i].show_contact();
          return;
       }
   }
   cout<<" No results found. Please try again. ";

}

void edit_contact(contact *data, int n){
system("clear");
int num;
cout<<"Edit customer ------------------------------- Enter the number of the customer that you wish to change:";
cin>>num;

for(int i = 0; i < n; i++)
{
if(data[i].getPhone()==num)
{
data[i].show_contact();
cout<<" Please enter the new customer information: "<<endl;
data[i].create_contact();
cout<<endl<<endl<<" Customer has been changed!";
return;
}
cout<<" No results found. Please try again. ";
}
}
/*
//Edit the customer information to update/correct information
void edit_contact()
{
int num;
bool found=false;
system("clear");
cout<<"Edit customer ------------------------------- Enter 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: "<<endl;
cont.create_contact();
int pos=-1*sizeof(cont);
fp.seekp(pos,ios::cur);
fp.write((char*)&cont,sizeof(cont));
cout<<endl<<endl<<" Customer has been changed!";
found=true;
}
}
fp.close();
if(found==false)
cout<<endl<<endl<<"Customer not found. Please try again.";
}
*/

void delete_contact(contact *data, int &n){

int num;
system("clear");
cout<<endl<<endl<<"Please enter the customers number: ";
cin>>num;

for (int i = 0 ; i<n; i++){
   if (data[i].getPhone() == num){
      for (int j = i+1; j<n; j++){
          data[j-1] = data[j];
         
      }
      cout<<endl<<endl<<" Customer Deleted.";
      n--;
      return;
   }
}
cout<<endl<<endl<<"Customer not found. Please try again.";
}


/*
//Delete a customer from the array
void delete_contact()
{
int num;
system("clear");
cout<<endl<<endl<<"Please enter the customers number: ";
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<<endl<<endl<<" Customer Deleted.";
}
*/


int read_contact(contact *data){

fp.open("contactBook.dat",ios::in);
if (fp != NULL){
   cout << "Error opening file ";
   return 0;
}
int count = 0;
while(fp.read((char*)&data[count],sizeof(contact)))
     count++;
return count;
}


void add_contact(contact *data, int &n){

   data[n].create_contact();
   cout<<endl<<endl<<"Customers has been added!";
   n++;

}


//Main module for the menu
int main(int argc, char *argv[])
{

contact data[100];

cout<<" - -";
cout<<" -- --";
cout<<" --- ---";
cout<<" ---- ----";
cout<<" ----- -----";
cout<<" ------ ------";
cout<<" ------- -------";
cout<<" ------ -------";
cout<<" ------ ------";
cout<<" ----- -----";
cout<<" ---- ----";
cout<<" --- ---";
cout<<" -- --";
cout<<" - -";


int n = read_contact(data);

for(;;)

{
int ch;


cout<<" ---- Welcome to the Contact Management System ----";
cout<<" MAIN MENU ---------------------- [1] Add a new customer [2] List all customers [3] Search for a customer [4] Edit a customer [5] Delete a customer [0] Exit ------------------ ";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 0: cout<<" Have a great day!";
write_contacts(data, n);
exit(0);
break;
break;
break;
case 1:add_contact(data,n);
break;
case 2:show_all_contacts(data,n);
break;
case 3:
int num;
system("clear");
cout<<" Phone: ";
cin>>num;
display_contact(data,num,n);
break;
case 4:edit_contact(data,n);
break;
case 5:delete_contact(data,n);
break;
default:
break;
}

int opt;
cout<<" Enter your choice: [1] Main Menu [0] Exit ";
cin>>opt;
switch (opt)
{
case 1:
system("clear");
continue;
case 0:
write_contacts(data,n);
exit(0);
}
}
return 0;
}

For Vector


/*For array */

#include<fstream>
#include<string>
#include<iomanip>
#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<vector>


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<<endl<<"Phone number: "<<ph;
cout<<endl<<"Name: "<<name;
cout<<endl<<"Address: "<<add;
cout<<endl<<"Email Address : "<<email;
}
long getPhone()
{
return ph;
}

void operator = (contact a){
   ph = a.ph;
   strcpy(name,a.name);
   strcpy(add, a.add);
   strcpy(email,a.email);
}
char* getName()
{
return name;
}
char* getAddress()
{
return add;
}
char* getEmail()
{
return email;
}
};
fstream fp;
//contact cont;
//Save customer information to the array
void write_contacts(vector<contact> &data)
{
fp.open("contactBook.dat",ios::out);
//cont.create_contact();
for (int i = 0; i<data.size(); i++){
   fp.write((char*)&data[i],sizeof(contact));
}
fp.close();
cout<<endl<<endl<<"Customers have been saved to file!";
//getchar();
}

void show_all_contacts(vector<contact> &data)
{
cout<<" -------------------------------- LIST OF CUSTOMERS -------------------------------- ";
   for (int i = 0; i<data.size(); i++){
      data[i].show_contact();
      cout<<endl<<"----------------------------------------------- "<<endl;
   }
}
/*
void show_all_contacts()
{
//system("clear");
cout<<" -------------------------------- LIST OF CUSTOMERS -------------------------------- ";
fp.open("contactBook.dat",ios::in);
while(fp.read((char*)&cont,sizeof(contact)))
{
cont.show_contact();
cout<<endl<<"----------------------------------------------- "<<endl;
}
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("clear");
cont.show_contact();
found=true;
}
}
fp.close();
if(found == false)
{
cout<<" No results found. Please try again.";}
getchar();
}

*/

void display_contact(vector<contact> &data, int x){

  
   for (int i = 0; i<data.size(); i++){
       if (data[i].getPhone() == x){
          system("clear");
          data[i].show_contact();
          return;
       }
   }
   cout<<" No results found. Please try again. ";

}

void edit_contact(vector<contact> &data){
system("clear");
int num;
cout<<"Edit customer ------------------------------- Enter the number of the customer that you wish to change:";
cin>>num;

for(int i = 0; i < data.size(); i++)
{
if(data[i].getPhone()==num)
{
data[i].show_contact();
cout<<" Please enter the new customer information: "<<endl;
data[i].create_contact();
cout<<endl<<endl<<" Customer has been changed!";
return;
}
cout<<" No results found. Please try again. ";
}
}
/*
//Edit the customer information to update/correct information
void edit_contact()
{
int num;
bool found=false;
system("clear");
cout<<"Edit customer ------------------------------- Enter 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: "<<endl;
cont.create_contact();
int pos=-1*sizeof(cont);
fp.seekp(pos,ios::cur);
fp.write((char*)&cont,sizeof(cont));
cout<<endl<<endl<<" Customer has been changed!";
found=true;
}
}
fp.close();
if(found==false)
cout<<endl<<endl<<"Customer not found. Please try again.";
}
*/

void delete_contact(vector<contact> &data){

int num;
system("clear");
cout<<endl<<endl<<"Please enter the customers number: ";
cin>>num;

for (int i = 0 ; i<data.size(); i++){
   if (data[i].getPhone() == num){
      for (int j = i+1; j<data.size(); j++){
          data[j-1] = data[j];
         
      }
      cout<<endl<<endl<<" Customer Deleted.";
      return;
   }
}
cout<<endl<<endl<<"Customer not found. Please try again.";
}


/*
//Delete a customer from the array
void delete_contact()
{
int num;
system("clear");
cout<<endl<<endl<<"Please enter the customers number: ";
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<<endl<<endl<<" Customer Deleted.";
}
*/


void read_contact(vector<contact> &data){

fp.open("contactBook.dat",ios::in);
if (fp != NULL){
   cout << "Error opening file ";
   return;
}
int count = 0;
contact cont;
while(fp.read((char*)&cont,sizeof(contact)))
     data.push_back(cont);

}


void add_contact(vector<contact> &data){

   contact cont;
   cont.create_contact();
   data.push_back(cont);
   cout<<endl<<endl<<"Customers has been added!";

}


//Main module for the menu
int main(int argc, char *argv[])
{

vector<contact> data;

cout<<" - -";
cout<<" -- --";
cout<<" --- ---";
cout<<" ---- ----";
cout<<" ----- -----";
cout<<" ------ ------";
cout<<" ------- -------";
cout<<" ------ -------";
cout<<" ------ ------";
cout<<" ----- -----";
cout<<" ---- ----";
cout<<" --- ---";
cout<<" -- --";
cout<<" - -";


read_contact(data);

for(;;)

{
int ch;


cout<<" ---- Welcome to the Contact Management System ----";
cout<<" MAIN MENU ---------------------- [1] Add a new customer [2] List all customers [3] Search for a customer [4] Edit a customer [5] Delete a customer [0] Exit ------------------ ";
cout<<"Enter your choice:";
cin>>ch;
switch(ch)
{
case 0: cout<<" Have a great day!";
write_contacts(data);
exit(0);
break;
break;
break;
case 1:add_contact(data);
break;
case 2:show_all_contacts(data);
break;
case 3:
int num;
system("clear");
cout<<" Phone: ";
cin>>num;
display_contact(data,num);
break;
case 4:edit_contact(data);
break;
case 5:delete_contact(data);
break;
default:
break;
}

int opt;
cout<<" Enter your choice: [1] Main Menu [0] Exit ";
cin>>opt;
switch (opt)
{
case 1:
system("clear");
continue;
case 0:
write_contacts(data);
exit(0);
}
}
return 0;
}