home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Languages / THIN C 2.0 / Projects / cdTracker / cdTracker.c next >
Encoding:
C/C++ Source or Header  |  1991-04-12  |  2.4 KB  |  163 lines  |  [TEXT/THIN]

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. #define MAX_ARTIST_CHARS    50
  5. #define MAX_TITLE_CHARS        50
  6.  
  7. struct CDInfo
  8. {
  9.     char            rating;
  10.     char            artist[ MAX_ARTIST_CHARS ];
  11.     char            title[ MAX_TITLE_CHARS ];
  12.     struct CDInfo    *next;
  13. } *gFirstPtr, *gLastPtr;
  14.  
  15.  
  16. /********************************  Flush  ***/
  17.  
  18. void    Flush()
  19. {
  20.     while ( getchar() != '\n' )
  21.         ;
  22. }
  23.  
  24.  
  25. /********************************  ReadLine  ***/
  26.  
  27. void    ReadLine( char *line )
  28. {
  29.     char c;
  30.     
  31.     while ( (c = getchar()) != '\n' )
  32.     {
  33.         *line = c;
  34.         line++;
  35.     }
  36.     
  37.     *line = 0;
  38. }
  39.  
  40.  
  41. /********************************  ListCDs  ***/
  42.  
  43. void    ListCDs()
  44. {
  45.     struct CDInfo    *curPtr;
  46.     
  47.     if ( gFirstPtr == NULL )
  48.     {
  49.         printf( "No CDs have been entered yet...\n" );
  50.         printf( "\n----------\n" );
  51.     }
  52.     else
  53.     {
  54.         curPtr = gFirstPtr;
  55.     
  56.         while ( curPtr != NULL )
  57.         {
  58.             printf( "Artist:  %s\n", curPtr->artist );
  59.             printf( "Title:   %s\n", curPtr->title );
  60.             printf( "Rating:  %d\n", curPtr->rating );
  61.     
  62.             printf( "\n----------\n" );
  63.     
  64.             curPtr = curPtr->next;
  65.         }
  66.     }
  67. }
  68.  
  69.  
  70. /********************************  AddToList  ***/
  71.  
  72. void    AddToList( struct CDInfo *curPtr )
  73. {
  74.     if ( gFirstPtr == NULL )
  75.         gFirstPtr = curPtr;
  76.     else
  77.         gLastPtr->next = curPtr;
  78.     
  79.     gLastPtr = curPtr;
  80.     curPtr->next = NULL;
  81. }
  82.  
  83.  
  84. /********************************  ReadStruct  ***/
  85.  
  86. struct CDInfo    *ReadStruct()
  87. {
  88.     struct CDInfo    *infoPtr;
  89.     int                num;
  90.     
  91.     infoPtr = malloc( sizeof( struct CDInfo ) );
  92.     
  93.     if ( infoPtr == NULL )
  94.     {
  95.         printf( "Out of memory!!!  Goodbye!\n" );
  96.         exit( 0 );
  97.     }
  98.     
  99.     printf( "Enter Artist's Name:  " );
  100.     ReadLine( infoPtr->artist );
  101.     
  102.     printf( "Enter CD Title:  " );
  103.     ReadLine( infoPtr->title );
  104.     
  105.     num = 0;
  106.     while ( ( num < 1 ) || ( num > 10 ) )
  107.     {
  108.         printf( "Enter CD Rating (1-10):  " );
  109.         scanf( "%d", &num );
  110.         Flush();
  111.     }
  112.     
  113.     infoPtr->rating = num;
  114.     
  115.     printf( "\n----------\n" );
  116.     
  117.     return( infoPtr );
  118. }
  119.  
  120.  
  121. /********************************  GetCommand  ***/
  122.  
  123. char    GetCommand()
  124. {
  125.     char    command = 0;
  126.     
  127.     while ( (command != 'q') && (command != 'n')
  128.                     && (command != 'l') )
  129.     {
  130.         printf( "Enter command (q=quit, n=new, l=list):  " );
  131.         scanf( "%c", &command );
  132.         Flush();
  133.     }
  134.     
  135.     printf( "\n----------\n" );
  136.     return( command );
  137. }
  138.  
  139.  
  140. /********************************  main  ***/
  141.  
  142. main()
  143. {
  144.     char            command;
  145.     
  146.     gFirstPtr = NULL;
  147.     gLastPtr = NULL;
  148.     
  149.     while ( (command = GetCommand() ) != 'q' )
  150.     {
  151.         switch( command )
  152.         {
  153.             case 'n':
  154.                 AddToList( ReadStruct() );
  155.                 break;
  156.             case 'l':
  157.                 ListCDs();
  158.                 break;
  159.         }
  160.     }
  161.     
  162.     printf( "Goodbye..." );
  163. }