home *** CD-ROM | disk | FTP | other *** search
/ C by Discovery (4th Edition) / C_By_Discovery_4th_Edition.tar / C_By_Discovery_4th_Edition / _DISK_ / ch11 / convert.c < prev    next >
C/C++ Source or Header  |  2005-06-16  |  841b  |  29 lines

  1. /*               convert.c
  2.  *
  3.  *   Synopsis  - Accepts the input of a string of digits and
  4.  *               converts those digits to a decimal integer.
  5.  *   Objective - To show the use of the facility isdigit() in
  6.  *               validating input.
  7.  */
  8. /* Include Files */
  9. #include <stdio.h>
  10. #include <ctype.h>                                 /* Note 1 */
  11.  
  12. /* Constant Definitions */
  13. #define BUF_SIZE  80
  14. int main( void )
  15. {
  16.      int index = 0, num = 0;
  17.      char inbuff[BUF_SIZE];
  18.  
  19.      printf( "Enter a string of decimal digits: " );
  20.      fgets( inbuff, BUF_SIZE, stdin );
  21.  
  22.      while ( isdigit( inbuff[index] ))             /* Note 2 */
  23.                                                    /* Note 3 */
  24.           num = 10*num + inbuff[index++] - '0';
  25.  
  26.      printf( "That number is %d.\n", num );
  27.      return 0;
  28. }
  29.