Question

In: Computer Science

You are asked to write a simple C++ phonebook application program. Here are the requirements for...

You are asked to write a simple C++ phonebook application program. Here are the requirements for the application.

  • read the contact information from a given input file (phonebook.txt) into a dynamically created array of Contact objects. Each line of the input line includes name and phone information of a contact. Assume that each name has a single part
  • Allow to perform operations on array of data such as search for a person, create a new contact or delete an existing contact

A sample run:

***MY PHONEBOOK APPLICATION***

Please choose an operation:

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): A

Enter name: MARY SMITH

Enter phone: 5062396

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): S

Enter name: MARY SMITH

Phone Number: 5062396

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): L

BARBARA BROWN 4059171

ELIZABETH JONES 2736877

LINDA WILLIAMS 3532665

PATRICIA JOHNSON 973437

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): D

Enter name: LINDA WILLIAMS

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): L

BARBARA BROWN 4059171

ELIZABETH JONES 2736877

PATRICIA JOHNSON 973437

A(Add) | S (Search) | D(Delete) |L(List) |Q(Quit): Q

Solutions

Expert Solution

code of header file "Contact.h"

using namespace std;

int pos = 0;
int capacity = 100;

class contact{
  public:
    string fName;
    string sname;
    string phone;
 static void listContacts(contact *vec){
  for(int i=0;i<pos;i++){
    cout<<vec[i].fName<<" "<<vec[i].sname<<" "<<vec[i].phone<<endl;
  }
}
static contact * addContact(contact *vec){
  contact c;
  cout<<"Enter name: ";
  cin>>c.fName>>c.sname;
  cout<<"Enter phone: ";
  cin>>c.phone;
  if(pos<capacity){
      vec[pos] = c;
      pos++;
    }
    else{
      capacity+=20;
      contact *temp = new contact[capacity];
      for(int i=0;i<pos;i++)
        temp[i] = vec[i];
      vec = temp;
      vec[pos] = c;
      pos++;
    }
  return vec;
}
static contact * searchContact(contact * vec){
  string f_name,s_name;
  cout<<"Enter name ";
  cin>>f_name>>s_name;
  for(int i=0;i<pos;i++){
    if(vec[i].fName==f_name && vec[i].sname==s_name){
      cout<<"Phone Number: "<<vec[i].phone<<endl;
      return vec;
    }
  }
  cout<<"Contact not found!"<<endl;
  return vec;
}
static contact * deleteContact(contact *vec){
  string f_name,s_name;
  cout<<"Enter name ";
  cin>>f_name>>s_name;
  for(int i=0;i<pos;i++){
    if(vec[i].fName==f_name && vec[i].sname==s_name){
      for(int j=i+1;j<pos;j++){
        vec[j-1] = vec[j];
      }
      pos--;
      return vec;
    }
}
  return vec;
}
};

Code for the implementation file containing main():

#include <iostream>
#include <vector>
#include <sstream>
#include <bits/stdc++.h>
#include "Contact.h"

using namespace std;
int main(){
  contact *vec = new contact[capacity];
  ifstream fin;
  fin.open("phonebook.txt");
  string line;
  while(fin){
    getline(fin,line);
    istringstream ss(line);
    contact record;
    ss>>record.fName>>record.sname>>record.phone;
    if(pos<capacity){
      vec[pos] = record;
      pos++;
    }
    else{
      capacity+=20;
      contact *temp = new contact[capacity];
      for(int i=0;i<pos;i++)
        temp[i] = vec[i];
      vec = temp;
      vec[pos] = record;
      pos++;
    }
  }
  pos--;
  char choice;
  do{
    cout<<"Please choose an operation:"<<endl;
    cout<<"A(Add)|S(Search)|D(Delete)|L(List)|Q(Quit):";
    cin>>choice;
    switch(choice){
      case 'A': vec = contact::addContact(vec);
            break;
      case 'S': contact::searchContact(vec);
            break;
      case 'D': vec = contact::deleteContact(vec);
            break;
      case 'L': contact::listContacts(vec);
    }
  }while(choice!='Q');
  fin.close();
  ofstream fout;
  fout.open("phonebook.txt");
  for(int i=0;i<pos;i++){
    fout<<vec[i].fName<<" "<<vec[i].sname<<" "<<vec[i].phone<<endl;
  }
  
}

output is as following:


Related Solutions

Write the program in C++ The Rebel food truck has asked you to write a program...
Write the program in C++ The Rebel food truck has asked you to write a program for them to do both inventory and sales for their food wagon. Ben and Dave run the food truck and they already have full time day jobs so they could really use your help. The food truck sells: hamburger, hotdogs, chilli, fries and soda. The truck has spots for 200 hamburger patties, 200 hot dogs, 75 hamburger buns, 75 hot dog buns, 500 ounces...
In this question, you are asked to write a simple java program to understand natural language....
In this question, you are asked to write a simple java program to understand natural language. The user will enter the input following the format: Name came to City, Country in Year. For example: Robin came to Montreal, Canada in 2009. Assume a perfect user will follow the exactly above formats for the inputs. Your program should be able to analyze the key words (Name, City, Country and Year) from the inputs and reorganize the outputs following format: Name stay...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file...
Program Requirements: Write a C++ program according to the following requirements: 1.   Open the data file Electricity.txt and read each column into an array (8 arrays total). 2.   Also create 2 arrays for the following: Total Fossil Fuel Energy (sum of all fossil fuels) Total Renewable Energy (sum of all renewable sources) Electricity.txt: Net generation United States all sectors monthly https://www.eia.gov/electricity/data/browser/ Source: U.S. Energy Information Administration All values in thousands of megawatthours Year   all fuels   coal       natural gas   nuclear  ...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: ·...
Directions: You are to write a C++ program that meets the instruction requirements below. Deliverables: · Your C++ source code file. (The file with the .CPP extension).No other files will be accepted. A screenshot of your program running. Program Instructions: Consider the following incomplete C++ program: #include int main() { … } 1. Write a statement that includes the header files fstream, string, and iomanip in this program. 2. Write statements that declare inFile to be an ifstream variable and...
1. [100] Create a C program for a number guessing game. Here are the requirements: a....
1. [100] Create a C program for a number guessing game. Here are the requirements: a. Your program generates a random number between -100 and 100 and keeps asking the user to guess the number until the user guesses it correctly. Your random number generator should be implemented as a C function which takes min and max values as input parameters and returns a random number between those values including the min and the max. b. If the user guesses...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte...
C/ C++ Preferably 1. Write a simple program where you create an array of single byte characters. Make the array 100 bytes long. In C this would be an array of char. Use pointers and casting to put INTEGER (4 byte) and CHARACTER (1 byte) data into the array and pull it out. YES, an integer can be put into and retrieved from a character array. It's all binary under the hood. In some languages this is very easy (C/C++)...
You can complete this with Eclipse (or BlueJ if you like). Here are the requirements: Write...
You can complete this with Eclipse (or BlueJ if you like). Here are the requirements: Write a Person class. A Person has properties of name and age. You construct a Person object by providing the name and age in that order. We want to be able to get and set the name. We want to be able to get the age and to increase the age by 1 when the person has a birthday. What will the object need to...
You are asked to design a relational database for a simple course registration software application for...
You are asked to design a relational database for a simple course registration software application for your school. The relational database must have the following information about the student, the course, and the registration, respectively StudentID, FirstName, LastName, DataOfJoining, and Major CourseNumber, CourseName,InstructorName, StartDate, EndDate, NumberOfCredits ReferenceID, StudentID,CourseID, DateOfRegistration Apply the following constrains while designing the database Each student in the database must be uniquely identifiable Each course listed in the database must be have unique CourseNumber Each course registration...
Write a C program that meets the following requirements. Uses a while loop to display the...
Write a C program that meets the following requirements. Uses a while loop to display the first 10 natural numbers (on one row, with a tab separating each number) Uses a while loop to find the sum of the second set of 10 natural numbers. Reads a user entry and displays all the natural numbers up to the user entry (on a column list with a new line separating each number). Finds and displays the sum of all natural numbers...
Write a code for simple racing game (using dots) on c program.
Write a code for simple racing game (using dots) on c program.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT