Computer Science 2611
Summer 2000
Laboratory Assignment 10

Handed out: 7/17/00

This assignment is designed to give you more experience with arrays and with reading files. It uses two-dimensional arrays.

Suppose that a file has been prepared containing names and telephone numbers. Each line contains a name (without any spaces) followed by a telephone number (without any spaces). For example, the file might be as follows.

Chuckie 111-2233
Angelica 662-1111
Tommy 111-4441
Phil 221-7654
Lil 221-4567

Your program should start by prompting for the name of the file that contains this data. Then it should read the file into two arrays, one that holds the names and the other that holds the telephone numbers. It should then ask for a name, and should print the corresponding telephone number. It should continue asking for names, and showing telephone numbers, until the string quit is typed. When a name that is not listed is asked for, the program should say that the name is not listed and continue. So a session with this program might look like this. The parts in black are typed by the computer, and the parts in blue are typed by the user.

  What is the name of the telephone number file?  phone.txt
  
  What name shall I look up?  Angelica
  The number for Angelica is 662-1111
  
  What name shall I look up? Tommy
  The number for Tommy is 111-4441
  
  What name shall I look up? Richard
  I am sorry, Richard is not listed.

  What name shall I look up? quit
Break your program into at least the following functions, and possibly more.

  1. A function that that reads the telephone book into two two-dimensional arrays, one holding the names and another holding the telephone numbers. There should be one name per row of each array.

  2. A function that looks up a name and returns the index where that name is found (and returns -1 if the name was not found).

  3. A function that reports the name and number to the user.

  4. A function that talks to the user, and contains the main loop, repeatedly asking for a name and reporting the information. This can be the main function.

Test your program by creating a test data file.

Hints

  1. Assume that names and phone numbers are no more than 15 characters long. But be sure to leave room for the null terminator. Also assume that there are no more than 100 names in the file. Make sure that both of these assumptions are easy to change.

  2. The names and numbers should be stored as arrays of strings. That is, each thing in the array is an array of characters. You can create an array of MAXENTRIES entries, with each entry being a string of length at most MAXLENGTH as follows.
            char names[MAXENTRIES][MAXLENGTH+1];
      
    Similarly, you can create an array to hold the numbers, by
            char numbers[MAXENTRIES][MAXLENGTH+1];
      
    Now names[0] is an array of characters, MAXLENGTH+1 bytes long, as is names[1], etc.

  3. Be careful to keep track of how many entries in the names table are actually in use. When you search for a name, you do not want to look at part of the names array that has no information in it.

  4. When you look up a name, and find it at index $k$ in the names array, you should find the corresponding number at the same index $k$ in the numbers array.

  5. After you open the file that contains the data, be sure to check whether the file exists. If it does not, your program should report that there is no such file.

  6. To read the names and phone numbers, just read strings using >>. If you put the read in a test, you can tell whether anything was available to read. You can write, for example,
         while(infile >> names[k]) 
      
    The "test" infile>>names[k] first reads a word into names[k], and then returns true if a word was found, and false if no word was found (because there was no more data).

    Do not try to read an entire line and then to break the line into the name and number. Instead, directly read a name into the names array and a number into the numbers array. Don't worry about the line breaks at all. They will be skipped over automatically.

  7. You will need to ask whether two strings are equal. Do not use operator == to test strings for equality. To ask whether null-terminated strings A and B are equal, use
           if(strcmp(A,B) == 0)
      
    strcmp(A,B) is 0 if strings A and B are the same. To use function strcmp, you need to include header file string.h.

    To ask whether a name is the string "quit", just write

          if(strcmp(name, "quit") == 0) 
      

  8. If you pass a two-dimensional array to a function, be sure to provide the correct physical size of the second index. For example, the heading of a search function might look like this.
         int search(char names[][MAXLENGTH+1], int n)
      
    It is critical that the correct physical size be given, or the program will not work.