home *** CD-ROM | disk | FTP | other *** search
/ C Programming Starter Kit 2.0 / SamsPublishing-CProgrammingStarterKit-v2.0-Win31.iso / tyc / list7_3.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-16  |  1.4 KB  |  58 lines

  1.    /* Demonstration on using scan() */
  2.  
  3.    #include <stdio.h>
  4.  
  5.    #define QUIT 4
  6.  
  7.    int get_menu_choice( void );
  8.  
  9.    main()
  10. {
  11.       int   choice    = 0;
  12.       int   int_var   = 0;
  13.       float float_var = 0.0;
  14.       unsigned unsigned_var = 0;
  15.  
  16.       while( choice != QUIT )
  17.       {
  18.           choice = get_menu_choice();
  19.  
  20.           if( choice == 1 )
  21.           {
  22.              puts( "\nEnter a signed decimal integer (i.e. -123)" );
  23.              scanf( "%d", &int_var );
  24.           }
  25.           if ( choice == 2 )
  26.           {
  27.               puts( "\nEnter a decimal floating point number (i.e. 1.23)" );
  28.               scanf( "%f", &float_var );
  29.           }
  30.           if ( choice == 3 )
  31.           {
  32.                puts( "\nEnter an unsigned decimal integer (i.e. 123)" );
  33.                scanf( "%u", &unsigned_var );
  34.           }
  35.       }
  36.       printf( "\nYour values are: int: %d  float: %f  unsigned: %u ",
  37.                                   int_var, float_var, unsigned_var );
  38.   }
  39.  
  40.   int get_menu_choice( void )
  41.   {
  42.       int selection = 0;
  43.  
  44.       do
  45.       {
  46.           puts( "\n1 - Get an signed decimal integer" );
  47.           puts( "2 - Get a decimal floating point number" );
  48.           puts( "3 - Get an unsigned decimal integer" );
  49.           puts( "4 - Quit" );
  50.           puts( "\nEnter a selection:" );
  51.  
  52.           scanf( "%d", &selection );
  53.  
  54.        }while ( selection < 1 || selection > 4 );
  55.  
  56.        return selection;
  57.   }
  58.