Exit function will not work. Can't figure out why? Here is the code, any suggestions are welcome.
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define clrscr() system("cls")
//structure for writing to and reading from file**
struct filebook
{
int code;
char title[20];
char author[20];
int pages;
}ebook;
//************************************************
//structure for creating the linked list**********
struct book
{
int code;
char title[20];
char author[20];
int pages;
int deleted;
struct book*next;
}*head,*work,*node,*previous,*found,libry;
FILE*library;
//VAR declarations
char title[20];
char ans;
int ad;
int buffer;
char author;
int ctr;
int choice;
int code;
//************************************************
/* FUNCTION PROTOTYPES*********************/
int main_menu();
void add_record(void);
void delete_record(void);
void search(void);
void display(void);
void exit_prog(void);
/*******************************************/
void main()
{
FILE *library;
int choice;
clrscr();
library = fopen("library.dat", "r+b"); //opening in binary mode for read/write
if (library == NULL)
{
printf("\n\nFile does not exist: Creating file");
library = fopen("library.dat", "w+b"); //creation in binary mode
head=NULL;//empty list
//key_wait();
getch();
}
else
{
/*Load the file contents in a linked list*******************/
head=work=(struct book *)malloc(sizeof(struct book));
fread(work,sizeof(struct filebook),1,library);
while(!feof(library))
{
work->deleted=0;
work->next=(struct book *)malloc(sizeof(struct book));
fread(work->next,sizeof(struct filebook),1,library);
if(!feof(library))
{
work=work->next;
}
else
{
work->next=NULL;//end of list
}
}
}
fclose(library);
do
{
choice=main_menu();
clrscr();
switch (choice)
{
case 1: add_record(); break;
case 2: delete_record(); break;
case 3: search(); break;
case 4: display(); break;
case 5: exit_prog(); break;
}
}
while (choice != '6');
}
/************************************************** **********/
int main_menu()
/************************************************** **********/
{
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: ");
scanf ("%d", &choice);
return choice;
}
/************************************************** **********/
void add_record(void)
/*
Task: Read from keyboard and add the record in the list
Note: The code must be unique and < 10000
*/
/************************************************** **********/
{
another:
system("cls");
printf ("\t\t\tTusket Municipal Library\n\n\n");
printf("Enter code:");
scanf("%d",&libry.code);
printf("\nEnter Title:");
scanf("%s",&libry.title);
printf("\nEnter Author:");
scanf("%s",&libry.author);
printf("\nEnter Number of Pages:");
scanf("%d",&libry.pages);
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;
library=fopen("library.dat","a+b");
fclose(library);
main_menu();
}
else
{
if(ans=='y')
{
work->next=(struct book*)malloc(sizeof(ebook));
work=work->next;
goto another;
}
else
{
if(ans!='n')
{
printf("Please Enter lower case y or n");
printf ("\n\nPress a key to continue...");
getch();
goto choice;
}
else
{
if(ans!='y')
{
printf("Please Enter lower case 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),(size_t)3,library);
fclose (library);
}
main_menu();
}
/************************************************** **********/
void delete_record(void)
/*
Tasks: Read the code input by the user
Search for that code in the list
Mark the record as DELETED or
Advise user of its non-existence
Note: Must check the value of DELETED field before
*/
/************************************************** **********/
{
// system("cls");
// printf ("\t\t\tTusket Municipal Library\n\n\n");
// printf ("Enter code of the book to be deleted: ");
// scanf ("%d", &code);
}
/************************************************** **********/
void search(void)
/*
Tasks: Read the code input by the user
Search for that code in the list
Display the record or advise user of its non-existence
Note: Must check the value of DELETED field
*/
/************************************************** **********/
{
int key;
system("cls");
printf ("\t\t\tTusket Municipal Library\n\n\n");
printf("Which book code do you want to search for?: ");
scanf("%d", &key);
printf("\n");
found=head;
while(found!= NULL && found->code!= key) //DO NOT reverse condition
{
found=found->next;
}
if (found == NULL)
{
printf ("Sorry, not in the list.");
printf("\n");
getch();
main_menu();
}
else
{
printf ("Your book has been found in the list.");
printf("\n");
getch();
main_menu();
}
}
/************************************************** **********/
void display(void)
/*
Task: Ask for the order (ASC or DESC)
CREATE a SORTED list
Display all records from the SORTED list
Release RAM taken by SORTED LIST
Note: Must check the value of DELETED field
*/
/************************************************** **********/
{
system("cls");
printf ("\t\t\tTusket Municipal Library\n\n\n");
printf("Press 1 for ascending; 2 for descending: ");
scanf("%d", ad);
if (head!=NULL)
{
if (ad==1)
{
work->next=(struct book*)malloc(sizeof(ebook));
work=work->next;
work->next=NULL;
fread(library,sizeof(ebook),1,library);
node=work;
work=head;
while(work->next!=NULL)
{
if (node->code<work->code)
{
buffer=work->code;
work->code=node->code;
node->code=buffer;
}
work=work->next;
}
}
work=head;
printf("\nContents of List:\n\n");
do
{
printf("%d\t",work->code);
work=work->next;
}while (work!=NULL);
}
if(head!=NULL)
{
if (ad==2)
{
work->next=(struct book*)malloc(sizeof(ebook));
work=work->next;
work->next=NULL;
fread(library,sizeof(ebook),1,library);
node=work;
work=head;
while(work->next!=NULL)
{
if (node->code>work->code)
{
buffer=work->code;
work->code=node->code;
node->code=buffer;
}
work=work->next;
}
}
work=head;
if (head!=NULL)
{
printf("\n\nContents of List (Reversed order):\n\n");
do
{
printf("%d\t",work->code);
work=work->next;
}while (work!=NULL);
}
}
}
/************************************************** **********/
void exit_prog(void)
/*
Task: Pause until user presses a key
*/
/************************************************** **********/
{
library = fopen("library.dat", "wb");
work=head;
while(work!=NULL)
{
//if(work->deleted==0) commented out because delete function not implemented yet
//{
fwrite(work,sizeof(struct filebook),1,library);
// }
work=work->next;
}
fclose(library);
}