home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch6 / break.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  1KB  |  52 lines

  1. /*               break.c
  2.  *
  3.  *   Synopsis  - Accepts input of a line of text and displays
  4.  *               the part of a line before the SENTINEL
  5.  *               character.
  6.  *
  7.  *   Objective - To illustrate the break statement.
  8.  */
  9.  
  10. /* Include Files */
  11. #include <stdio.h>
  12.  
  13. /* Constant Definitions */
  14. #define EOLN      '\n'
  15. #define SENTINEL  '#'
  16.  
  17. /* Function Prototypes */
  18. void readtosentinel( char eoln, char sentinel );
  19. /*   PRECONDITION:  eoln and sentinel are character variables 
  20.  *                    containing the end of line character and the 
  21.  *                    sentinel value respectively.
  22.  *
  23.  *   POSTCONDITION: Reads a line of text from standard input. Stops 
  24.  *                  when either sentinel is encountered or eoln is 
  25.  *                  read. 
  26.  */
  27. int main( void )
  28. {
  29.  
  30.      printf( "The sentinel character is %c.\n", SENTINEL );
  31.      printf( "Enter a line with a sentinel in it.\n\n" );
  32.  
  33.      readtosentinel( EOLN, SENTINEL );
  34.  
  35.      printf( "\n\nThat was the first part of the line.\n" );
  36.      return 0;
  37. }
  38.  
  39. /*******************************readtosentinel()****************/
  40.  
  41. void readtosentinel( char eoln, char sentinel )
  42. {
  43.      int iochar;
  44.  
  45.      while ( ( iochar = getchar() ) != eoln ) {
  46.           if ( iochar == sentinel ) {
  47.                break;                             /* Note 1 */
  48.           }
  49.           putchar( iochar );
  50.      }
  51. }
  52.