A very common usage of loops is the repetitive reading of data . This reading may be from the keyboard or from an existing data file which contains a long list of numbers. In the case of reading from a data file, an approach is to let the program read to the end of the file or until an improper data field is encountered.
|
|
mydata.eof( ) | will evaluate to TRUE when we reach the end
of file ( immediately after mydata >> ..) |
mydata.fail( ) |
will evaluate to TRUE when improper data is
encountered ( immediately after mydata >> ..) |
The following is an annotated example for several numbers, which are read from a file and outputted to the screen, while their sum is calculated. |
© 2001 J.Y. Kazakia. All rights reserved