Phonebook

Overview We will create a phonebook application that will store name, number pairs, allow lookup by name or number, and provide persistent storage of data between application uses.

This assignment will provide experience with:

Getting Started To get started several things must be done:
  • Update the shared working copy
  • Create a directory in your working copy for the project
  • Copy the provided file phonebook_main.cpp
  • Copy the provided make file
  • Create empty files to type you code into
In the following commands the # deonotes a comment, you do not need to type the comment with the command.

Update the shared working copy.

 cd                        # Go to your home directory
 cd cs23021_shared         # Go into the shared working copy
 svn update                # Do the update

Create directory for the project.

 cd                                           # Go to your home directory
 cd cs23021                                   # Go into your working copy
 svn mkdir Phonebook                          # Create dir for the project
 ll                                           # Notice the new directory
 svn commit -m "Phonebook project directory"  # Commit

A file with the main function is provided in a file named phonebook_main.cpp.

Copy the file phonebook_main.cpp.

 cd                                                     # Go to your home directory
 cd cs23021                                             # Go into your working copy
 cd Phonebook                                           # Go into new directory
 cp ../../cs23021_shared/Phonebook/phonebook_main.cpp . # Copy provided file
 svn add phonebook_main.cpp                             # Add file with main
 svn commit -m "Phonebook project provided file"        # Commit

Copy the Makefile.

 cd                                            # Go to your home directory
 cd cs23021                                    # Go into your working copy
 cd Phonebook                                  # Go into Phonebook directory
 cp ../../cs23021_shared/Phonebook/Makefile .  # Copy make file to current dir
 svn add Makefile                              # Add make file
 svn commit -m "Phonebook project make file"   # Commit

Create empty files to get started.

 touch phonebook.h phonebook.cpp            # Create empty files
 svn add phonebook.h phonebook.cpp          # Add empty files
 svn commit -m "Empty initial files"        # Commit

Implement as described below.

Files Three files are used:
  1. phonebook_main.cpp - provided
  2. phonebook.h - you create, contains the definitions for the specified structures and the declarations for the specified functions
  3. phonebook.cpp - you create, contains the definitions for the specified functions
Provided The following functions are in the provided file:
int main();
void displayPrompt();
Note that you do not write a main() function.
Structures Use the following two structures:
 struct PB_Entry
 {
     std::string _name;
     int         _number;
 };

 struct Phonebook
 {
     std::string           _file_name;
     std::vector<PB_Entry> _entries;
 };
Functions Put the structure definitions and function declarations in the file phonebook.h.

Define the following functions in the file phonebook.cpp.

void input(Phonebook&);
  • Open a file using the filename in the Phonebook object, error checking should be done. The file will be of the form:
      nameA  23
      nameB  931
      nameC  3526
      nameD  222
    
    Create a file with the above structure, use whatever contents you like.
  • Read in the pairs of values, use the following while loop condition:
    in >> inStr && in >> inVal
  • Create a PB_Entry object and store the input values in it.
  • Use push_back() to put the PB_Entry object in the phonebook's vector.
  • Close the file.
int lookupName(const Phonebook&, const std::string&)
  • Search the vector for an occurrence of the second argument. Use a sequential search.
  • Return the index in the vector if found, else return -1.
int lookupNumber(const Phonebook&, int)
  • Search the vector for an occurrence of the second argument. Use a sequential search.
  • Return the index in the vector if found, else return -1.
void addToLists(Phonebook&, const std::string&, int)
  • Create a PB_Entry object and store the parameter values in it.
  • Use push_back() to put the PB_Entry object in the phonebook's vector.
void outputAll(const Phonebook&)
  • Output all the name/number pairs, each pair on its own line. The format should be similar to the input file however the exact format is not important.
void save(const Phonebook&)
  • Open a file using the filename in the Phonebook object, error checking should be done.
  • Write the name/number pairs to the file one pair per line. The format should be similar to the input file.
  • Close the file.
Compiling Commands the make file provides:
  make          # Compile the program, executable is named phonebook
  make pbfile   # Generate an phonebook file named pb.txt
  make clean    # Remove .o files and the executable
Running Run by specifying the phonebook file on the command line:
  ./phonebook phonebook_file
Implementing Suggested implementation stages.
  1. In the header file put the struct definitions and the function declarations.
  2. Write stubs for each function, compile, and run. (A stub is a function with an empty body and a return with a reasonable value.)
  3. Compile. The program should compile at this point. If it doesn't compile now it won't compile later and it will be harder to figure out why later.
  4. Commit.
     svn commit -m "structs defined, stubs in, compiles"     # Commit
    
    Commit regularly.
  5. Implement the input() function. Temporarily put a cout after the values are stored so that you may see the values that are actually input. Including extra output statements is a handy debugging tool, however do not leave them in the finished product.
  6. Compile and run. Commit.
  7. Implement the outputAll() function. Delete the cout from input().
  8. Compile and run. Test the outputAll() function. Commit.
  9. Implement the remaining functions, one at a time, in any order.
Requirements The general program requirements apply.
  • Your code must work with the given main function file.
  • Do not leave any debugging outputs in the code you turn in.
Turning In Do the following:
  • Have the source code in the repository.