Home Segments Index Top Previous Next

597: Practice

The input operator reads character sequences only up to the first whitespace character, which makes it awkward to deal with input data such as the following, in which the hyphens have been replaced by spaces and comments have been added:

TPW E 783 
PPU B 422       ; Needs door repair. 
NYC B 988 
NYC T 988       ; Wheel going bad. 
TPW C 271       ; Not to be confused with the Ritz Carlton. 

Fortunately, C++ offers a variety of alternative ways to read data. One such alternative is to use the getline function, a member function of the cin class:

getline(name of character array, maximum characters to be read) 

Write a program, using getline, that reads lines such as those just shown and that displays only the serial numbers, without the comments, but with line numbers, as in the following example:

TPW E 783       1 
PPU B 422       2 
NYC B 988       3 
NYC T 988       4 
TPW C 271       5