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?