Thread: Simply Library Catalog Program not working

  1. #16
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    Here is my latest version:

    Code:
    #include "stdio.h"
    #include "conio.h"
    #include "string.h"
    
    void enter(char lib[100][3][50]);
    void findAuthor(char lib[100][3][50]);
    void findTitle(char lib[100][3][50]);
    
    int main(void)
    {
         char choice[1];
         char catalog[100][3][50];        
              
         do {
             printf("Card Catalog:\n");
             printf("1. Enter\n2. Search by Author\n3. Search by Title\n4. Quit\n");
             printf("Enter your choice here: ");  
             
             fgets(choice,2,stdin);       
         
             while(choice[0]!='1'&&choice[0]!='2'&&choice[0]!='3'&&choice[0]!='4'){
             printf("\nInvalid choice. Try again.");
             fgets(choice,2,stdin);
             }
             
             switch(choice[0]) {
               case('1'):
                 enter(catalog);
                 break;
            
               case('2'):
                 findAuthor(catalog);
                 break;
               
               case('3'):
                 findTitle(catalog);
                 break;
              }
            } while(choice[0]!='4');
            
            printf("Thank you for using the Library Catalog. Press any key to exit.");
            getch();  
            return 0;      
    }
            
    void enter(char lib[100][3][50])
    {
         int i;          
         
         for(i=0;i<3;i++){
             printf("\nEnter the title: ");           
             if(!strcmp(fgets(lib[i][1],50,stdin),"\n")) break;               
             printf("\nEnter the author: ");
             fgets(lib[i][2],50,stdin);  
             printf("\nEnter the publisher: ");
             fgets(lib[i][3],50,stdin);       
         }
    }
    
    void findAuthor(char lib[100][3][50])
    {
         char author[50];
         int i;
         
         printf("Enter the author: \n");
         fgets(author,50,stdin);
         
         for(i=0;*lib[i][1];i++){
            if(!strcmp(author,lib[i][1])) {
              printf("Title: %.50s\n",lib[i][2]);
              printf("Publisher: %.50s\n",lib[i][3]);
              break;
            }    
            else printf("No matches found.\n");     
         }     
         
    }
    
    void findTitle(char lib[100][3][50])
    {
         char title[50];
         int i;
         
         printf("Enter the title: \n");
         fgets(title,50,stdin);
         
         for(i=0;*lib[i][1];i++){
            if(!strcmp(title,lib[i][2])){
              printf("Author: %.50s\n",lib[i][1]);
              printf("Publisher: %.50s\n",lib[i][3]);
              break;
              }
            else printf("No matches found.\n");        
         }     
    }
    Even with fgets I still have to press enter at the menu (after my choice of course) before the menu responds, and I still have the problem of \n being in the input buffer because the loop for the enter function breaks immediately before I can even enter an author for the first time. How do I remove \n from the input buffer?

  2. #17
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You remove \n from the input buffer by not being an idiot about how you get input.
    Code:
    char choice[80];
    fgets(choice,80,stdin);
    Of course if they lean on the key they can still leave stuff in the buffer, but then it becomes their fault.

  3. #18
    Registered User
    Join Date
    Jan 2009
    Posts
    22
    OK, it works fine now, thanks a lot!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. loading a 32-bit library into 64-bit linux program
    By Elkvis in forum Linux Programming
    Replies: 11
    Last Post: 03-26-2009, 10:58 AM
  2. Client-server system with input from separate program
    By robot-ic in forum Networking/Device Communication
    Replies: 3
    Last Post: 01-16-2009, 03:30 PM
  3. Replies: 4
    Last Post: 03-26-2008, 08:48 AM
  4. Compiling Library Into Program.
    By Shogun in forum Linux Programming
    Replies: 10
    Last Post: 11-30-2004, 01:29 PM
  5. character occurrence program not working
    By Nutshell in forum C Programming
    Replies: 6
    Last Post: 01-21-2002, 10:31 PM