This is what I have so far. I'm stuck on what to do next. Can't seem to get book entries to save. Think I have to create a link list but not sure how. Also attached is the problem I'm trying to solve.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct book
{
int code;
char title[20];
char author[20];
int pages;
struct book*next;
}*head,*work,libry;
FILE*library;
main()
{
int pick;
char ans;
menu:
library=fopen("library.dat","wb");
fread(&library,sizeof(struct book),1,library);
fclose(library);
system("cls");
printf ("\t\t\tTusket Municipal Library\n\n\n");
printf ("\tPlease make a selection...\n\n");
printf ("\t1.) Enter a book in the inventory\n");
printf ("\t2.) Delete a book from the inventory\n");
printf ("\t3.) Search the inventory for a book\n");
printf ("\t4.) Display the book inventory\n");
printf ("\t5.) Exit the program\n\n");
printf ("\tEnter your selection:1,2,3,4 or 5 \n");
scanf ("%d", &pick);
while (pick==1)
{
system("cls");
//create list
head=work=(struct book*)malloc(sizeof(struct book));
work=head;
another:
printf("Enter code:");
scanf("%d",&libry.code);
printf("Enter Title:");
printf("\n");
gets(libry.title);
printf("Enter Author:");
gets(libry.author);
printf("Enter Number of Pages:");
scanf("%d",&libry.pages);
fwrite (&library,sizeof(struct book),1,library);
choice:
printf("\n\nDo you want to add another book?:");//answer must not be a capital
scanf("%s",&ans);
if(ans=='n')
{
work->next=NULL;
goto menu;
}
else
{
if(ans=='y')
{
work->next=(struct book*)malloc(sizeof(struct book));
work=work->next;
goto another;
}
else
{
if(ans!='n')
{
printf("Please Enter y or n");
printf ("\n\nPress a key to continue...");
getch();
goto choice;
}
else
{
if(ans!='y')
{
printf("Please Enter y or n");
printf ("\n\nPress a key to continue...");
getch();
goto choice;
}
}
}
}
library=fopen("library.dat","a+b");
if((library=fopen("library.dat","a+b"))!=NULL)
{
fwrite (&library,sizeof(struct book),1,library);
fclose (library);
}
}
while (pick==2)
{
printf("Not working yet!!!!!");
printf ("\n\nPress a key to continue...");
getch();
goto menu;
}
while (pick==3)
{
printf("Not working yet!!!!!");
printf ("\n\nPress a key to continue...");
getch();
goto menu;
}
while(pick==4)
{
printf("Not working yet!!!!!");
printf ("\n\nPress a key to continue...");
getch();
goto menu;
}
while(pick==5)
{
fclose(library);
return 0;
}
return 0;
}
If you send me a working version of this program and you want to collect: send mailing address and the checks in the mail!