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

C++: Create a simpe address book using arrays that allows a user to search, add,

ID: 3815424 • Letter: C

Question

C++: Create a simpe address book using arrays that allows a user to search, add, print, or delete an entry. The user should be allowed to search by first name, last name, phone number or address. The user should be asked to enter the file name. The array should hold 100 entries. The header file should contain two structs and one enum type.**** DO NOT USE LINKED LIST, NODES,OR VECTORS***
File:
Bob, Smith, 123 456 7890 101 Market Street, Orlando, Fl, 12345 Susan, King, 234 568 1234 208 Main Drive, Nashville, TN, 45675 C++: Create a simpe address book using arrays that allows a user to search, add, print, or delete an entry. The user should be allowed to search by first name, last name, phone number or address. The user should be asked to enter the file name. The array should hold 100 entries. The header file should contain two structs and one enum type.**** DO NOT USE LINKED LIST, NODES,OR VECTORS***
File:
Bob, Smith, 123 456 7890 101 Market Street, Orlando, Fl, 12345 Susan, King, 234 568 1234 208 Main Drive, Nashville, TN, 45675
File:
Bob, Smith, 123 456 7890 101 Market Street, Orlando, Fl, 12345 Susan, King, 234 568 1234 208 Main Drive, Nashville, TN, 45675

Explanation / Answer

// AddressBook.hpp

#include <string>
enum states_t {FI,TN,AK,AS};
std::string states_map[] = {"FI","TN","AK","AS"};
struct address_type
{
   std::string line;
   states_t state;
   int zip_code;
};
struct entry
{
   std::string first_name;
   std::string last_name;
   address_type add;
   int phone;
   bool isValid;
   entry():isValid(0){}
};

// AddressBook.cpp

#include "AddressBook.hpp"
#include <iostream>
#include <fstream>
using namespace std;
const int max_size = 100;
entry address_book[max_size];
ofstream myfile;
bool isEqual(address_type add1, address_type add2)
{
   return (add1.zip_code == add2.zip_code) && (add1.state == add2.state) && (add2.line == add1.line);
}
int add_entry(string first_name,string last_name, int phone, address_type add)
{
   int i = 0;
   while(i<max_size)
   {
       if (!address_book[i].isValid)
       {
           address_book[i].first_name = first_name;
           address_book[i].last_name = last_name;
           address_book[i].phone = phone;
           address_book[i].add = add;
           address_book[i].isValid = true;
           return 1;
       }
       i++;
   }
   return -1;
}
void print_details(int i){
   if(i<0||i>max_size)cout<<"Not found"<<endl;
   else{
       cout<<address_book[i].first_name<<", ";
       cout<<address_book[i].last_name<<", ";
       cout<<address_book[i].phone<<endl;;
       cout<<address_book[i].add.line<<", "<<states_map[address_book[i].add.state];
       cout<<", "<<address_book[i].add.zip_code<<endl;
   }
}
void print_details_file(int i){
   if(i<0||i>max_size)myfile<<"Not found"<<endl;
   else{
       myfile<<address_book[i].first_name<<", ";
       myfile<<address_book[i].last_name<<", ";
       myfile<<address_book[i].phone<<endl;;
       myfile<<address_book[i].add.line<<", "<<states_map[address_book[i].add.state];
       myfile<<", "<<address_book[i].add.zip_code<<endl;
   }
}
void print_add_book(bool to_file = 0)
{
   int i = 0;
   while(i<max_size)
   {
       if (address_book[i].isValid){
           if(!to_file){
               print_details(i);
           }else{
               print_details_file(i);
           }
       }
       i++;
   }
}
int search_by_first_name(string name)
{
   int i = 0;
   while(i<max_size)
   {
       if (address_book[i].isValid and address_book[i].first_name == name){
           return i;
       }
       i++;
   }  
   return -1;
}

int search_by_last_name(string name){
   int i = 0;
   while(i<max_size){
       if (address_book[i].isValid and address_book[i].last_name == name){
           return i;
       }
       i++;
   }  
   return -1;
}
int search_by_phone(int phone){
   int i = 0;
   while(i<max_size){
       if (address_book[i].isValid and address_book[i].phone == phone){
           return i;
       }
       i++;
   }  
   return -1;
}
int search_by_address(address_type add){
   int i = 0;
   while(i<max_size){
       if (address_book[i].isValid and isEqual(add, address_book[i].add)){
           return i;
       }
       i++;
   }  
   return -1;
}

int main(){
   address_type add;
   add.line = "101 Market Street, Orlando";
   add.state = FI;
   add.zip_code = 123456;
   add_entry("Bob","Smith",123456789,add);
   print_add_book();
   add.line = "208 Main Drive, Nashville";
   add.state = TN;
   add.zip_code = 45675;
   add_entry("Susan","King",2345681234,add);
   print_add_book();
   print_details(search_by_address(add));
   print_details(search_by_first_name("Shubham"));
   cout<<"enter file name"<<endl;
   char* file_name;  
   cin>>file_name;
   myfile.open (file_name);
   print_add_book(1);
   myfile.close();
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote