How to write a c program card catalog in C programming ?
Solution:
#include <stdio.h>
#include <string.h>
#define MAX 100
void display(int i);
void addNew();
void search();
void showAll();
int top=0; /*last created value*/
struct catalog
{
char name[80];
char author[80];
char pub[80];
char date[80];
char ed[10];
} cat[MAX];
int main()
{
int choice;
int i;
printf("1.New catalog\n");
printf("2.Search by title\n");
printf("3.Show All\n");
printf("4.Exit\n");
do
{
printf("Choose your selection:\n");
scanf("%d", &choice);
getchar();
if(choice < 1 || choice > 3)
printf("Invalid Selection\n");
else if(choice == 1)
addNew();
else if(choice == 2)
search();
else if(choice == 3)
showAll();
} while(choice != 4);
return 0;
}
void display(int i)
{
printf("%s\n",cat[i].name);
printf("by %s\n",cat[i].author);
printf("published by %s\n",cat[i].pub);
printf("copyright: %s, %s edition\n\n",cat[i].date,cat[i].ed);
}
void showAll()
{
int i;
for(i = 0; i < top; i++)
{
display(i);
printf("\n");
}
}
void addNew(void)
{
int i;
char temp[80];
printf("Enter title:\n");
gets(cat[top].name);
printf("Enter author:\n");
gets(cat[top].author);
printf("Enter publisher:\n");
gets(cat[top].pub);
printf("Enter copyright date:\n");
gets(temp);
printf("Enter edition:\n");
gets(cat[i].ed);
top++;
}
void search(void)
{
int i,found=1;
char title[80];
printf("Enter the title of the book you want to search:\n");
gets(title);
for(i=0;i<=top;i++)
if(!strcmp(title,cat[i].name))
{
display(i);
found=0;
}
if(found) printf("Sorry! Book not found.\n");
}