home *** CD-ROM | disk | FTP | other *** search
/ Microsoft Programmer's Library 1.3 / Microsoft-Programers-Library-v1.3.iso / sampcode / c / other / learn / atonum.c < prev    next >
Encoding:
C/C++ Source or Header  |  1988-10-28  |  1.4 KB  |  55 lines

  1. /* ATONUM.C illustrates string to number conversion functions including:
  2.  *      atof            atoi            atol            gcvt
  3.  *
  4.  * It also illustrates:
  5.  *      cgets           cputs
  6.  */
  7.  
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <conio.h>
  11.  
  12. #define MAXSTR 100
  13.  
  14. char cnumbuf[MAXSTR] = { MAXSTR + 2, 0 };
  15. #define numbuf cnumbuf + 2          /* Actual buffer starts at byte 3 */
  16. char tmpbuf[MAXSTR];
  17.  
  18. /* Numeric input and output without printf. */
  19. main()
  20. {
  21.     int     integer;
  22.     long    longint;
  23.     float   real;
  24.  
  25.     /* Using cgets (rather than gets) allows use of DOS editing keys
  26.      * (or of editing keys from DOS command line editors).
  27.      */
  28.     cputs( "Enter an integer: " );
  29.     cgets( cnumbuf );
  30.     cputs( "\n\r" );                /* cputs doesn't translate \n     */
  31.     integer = atoi( numbuf );
  32.     strcpy( tmpbuf, numbuf );
  33.     strcat( tmpbuf, " + " );
  34.  
  35.     cputs( "Enter a long integer: " );
  36.     cgets( cnumbuf );
  37.     cputs( "\n\r" );
  38.     longint = atol( numbuf );
  39.     strcat( tmpbuf, numbuf );
  40.     strcat( tmpbuf, " + " );
  41.  
  42.     cputs( "Enter a floating point number: " );
  43.     cgets( cnumbuf );
  44.     cputs( "\n\r" );
  45.     real = atof( numbuf );
  46.     strcat( tmpbuf, numbuf );
  47.     strcat( tmpbuf, " = " );
  48.  
  49.     gcvt( integer + longint + real, 4, numbuf );
  50.     strcat( tmpbuf, numbuf );
  51.     strcat( tmpbuf, "\n\r" );
  52.  
  53.     cputs( tmpbuf );
  54. }
  55.