14.8.1. Rolodex Problem

Rolodex™ 67236 Rotary Business Card File
An image of a Rolodex. By Poolcode (Own work) [CC BY-SA 3.0], via Wikimedia Commons

"A Rolodex is a rotating file device used to store business contact information." It consists of an axle or shaft with information cards attached at one edge. The cards spin around the axle, allowing a user to read the cards' information easily. The cards typically contain contact information such as an address and phone number. Our task is to write a simple program that implements a Rolodex.

Our program will read a text file one line at a time. Each line in the file will correspond to one Rolodex card. Each line will contain three separate data items: (a) a person's name, (b) the person's address, (c) the person's telephone number. Each data item or field is separated from the other fields by a colon character.

The program must be able to locate the colon, separate the individual fields into separate strings, and then produce a well-formatted table displayed on the screen (or in the console window). The process of separating data into individual components is called parsing and is a common part of many different computer problems.

There are many ways of parsing input. For example, compilers read a file one character at a time and assemble the characters into groups representing parts of a program. Each group of meaningful characters is called a token. Another way of parsing input is to read large blocks of text and then search for the characters that separate the tokens. The characters that separate the tokens are called delimiters. For the Rolodex problem, the straightforward format of the input file allows us to parse the input in yet a different way: We'll use an overloaded version of the getline function to read characters from the file into a string, but the function will stop reading when it reads (and discards) the colon delimiter.

Bill Gates:1 Microsoft Way, Redmond, WA:(403) 123-4567
Cranston Snort:1600 Pennsylvania Ave:(306) 678-9876
Albert Einstein:Princeton, NJ:(456) 123-8765
John Smith:123 Elm St.:801-555-1234
rolodex.txt. The input for the Rolodex programs is a line-oriented file that forms a simple database. Each line in the file corresponds to one Rolodex card and to one database record. Each line is divided into three bits of contact information, corresponds to three database fields: name, address, and phone number.

Downloadable: rolodex.txt

Locating rolodex.txt

The next two sections develop two programs, the first based on the string class and the second based on C-strings, that read rolodex.txt and produce the formatted output shown below. First, create the Visual Studio projects for each program. Then copy rolodex.txt into each project folder - that is, into the folder containing the source code (.cpp) files.

If you create rolodex.txt by copying and pasting from Figure 2 or the downloadable file, make sure that there are no empty lines - even at the end of the file.

Bill Gates          1 Microsoft Way, Redmond, WA       (403) 123-4567
Cranston Snort      1600 Pennsylvania Ave              (306) 678-9876
Albert Einstein     Princeton, NJ                      (456) 123-8765
John Smith          123 Elm St.                        801-555-1234
Formatted output from the Rolodex programs.