This is what i've got so far MK27
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 30
/* global variables */
typedef struct
{
char ISBN[20];
char Author[SIZE];
char Title[SIZE];
}record_structure;
FILE *record_file;
int choice;
record_structure record_array[8];
int num_records;
int searchresult;
char searchtitle[SIZE];
/* function prototypes */
void intro();
void menu();
void close();
void add_book();
void option2();
void search_by_title();
void display_all_records();
void screen_pause();
/*******************************************************************************************/
int main()
{
intro();
do
{
menu();
}
while (choice!=5);
close();
return 0;
}
/*******************************************************************************************/
void intro()
{
system("cls");
printf("\n\n\n\n\n\n\tWelcome to Forth Valley's Collection of Technical Books");
screen_pause();
}
/*******************************************************************************************/
void add_book()
{
record_file = fopen("C:\\bookfile.bin","wb");
system("cls");
printf( "\n\nAdd book details");
for(num_records=0;num_records<=7;num_records++)
{
printf( "\n\nISBN: ");
fflush(stdin);
fgets(record_array[num_records].ISBN, 20, stdin);
printf( "\n\nAuthor: ");
fflush(stdin);
fgets(record_array[num_records].Author, SIZE, stdin);
printf( "\n\nTitle: ");
fflush(stdin);
fgets(record_array[num_records].Title, SIZE, stdin);
printf( "------------------------------------" );
fwrite(&record_array[num_records],sizeof(record_structure),1,record_file);
}
printf( "\n\nYou have succesfully inputed the details of 8 books" );
fclose(record_file);
screen_pause();
}
/*******************************************************************************************/
void option2()
{
system("cls");
printf("Search for ISBN ");
screen_pause();
}
/*******************************************************************************************/
void search_by_title()
{
record_structure tmp;
printf ("Please enter the title of the book you are searching for: ");
scanf("%[^\n]%*c", &searchtitle);
record_file = fopen("C:\\bookfile.bin","rb");
while (fread(&tmp,sizeof(record_structure),1,record_file)) {
fprintf(stderr, "---> %s ||||| %s ||||| %s <---\n", tmp.Title, tmp.Author, tmp.ISBN);
if (strcmp(searchtitle,tmp.Title) == 0)
printf("Title: %s Author %s ISBN %s\n", tmp.Title, tmp.Author, tmp.ISBN);
}
fclose(record_file);
}
/*******************************************************************************************/
void display_all_records()
{
record_file = fopen("C:\\bookfile.bin","rb");
system("cls");
printf("\n\t\t\tDetails of all books");
printf("\n\n\n ID NO\t\tISBN\t\t\tAuthor\t\t\tTitle");
num_records=0;
fread(&record_array[num_records],sizeof(record_structure),1,record_file);
while(!feof(record_file))
{
printf("\n%3i %21s %47s %79s ",num_records,record_array[num_records].ISBN,
record_array[num_records].Author,record_array[num_records].Title);
fflush(stdin);
num_records++;
fread(&record_array[num_records],sizeof(record_structure),1,record_file);
}
screen_pause();
fclose(record_file);
}
/*******************************************************************************************/
void close()
{
system("cls");
printf("\n\n\n\n\n\n\tThank you for using this program");
screen_pause();
}
/*******************************************************************************************/
void menu()
{
system("cls");
printf("\n\nMENU");
printf("\n\n1 - Add new entry: ");
printf("\n2 - Book search by ISBN: ");
printf("\n3 - Book search by title: ");
printf("\n4 - Show details off all books stored: ");
printf("\n5 - Exit Program:");
printf("\n\nEnter your choice ");
scanf("%i", &choice);
while((choice<1)||(choice>5))
{
printf("\n\nYou eneterd an invalid number please enter a valid number");
printf("\n\n\tEnter your choice: ");
scanf("%i", &choice);
}
switch(choice)
{
case 1 : add_book();break;
case 2 : option2(); break;
case 3 : search_by_title(); break;
case 4 : display_all_records() ; break;
case 5 : break;
}
}
/*******************************************************************************************/
void screen_pause()
{
printf("\n\n\tPress any key to continue.....");
getch();
}