I need to implement two searches into this program, a search for an ISBN and a search for a title but i have no idea how to,o can someone please do it for me or set me off in the correct direction???
Thanks
Code:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SIZE 40
/* 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 option3();
void display_all_records();
void screen_pause();
/*******************************************************************************************/
int main()
{
intro();
do
{
menu();
}
while (choice!=5);
close();
return 0;
}/* end of main function */
/*******************************************************************************************/
void intro()
{
system("cls");
printf("\n\n\n\n\n\n\tWelcome to Forth Valley's Collection of Technical Books");
screen_pause();
}/*end of intro function*/
/*******************************************************************************************/
void add_book()
{
record_file = fopen("C:\\bookfile.bin","wb");
system("cls");
printf( "\n\nAdd book details");
for(num_records=0;num_records<=9;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();
}/* end of dummy menu option 1 function */
/*******************************************************************************************/
void option2()
{
system("cls");
printf("Search for ISBN ");
screen_pause();
}/* end of dummy menu option 2 function */
/*******************************************************************************************/
void option3()
{
printf ("Please enter the title of the book you are searching for: ");
// Declare searchresult as int and searchisbn as string:
searchresult = strcmp(record_array[num_records].ISBN,searchtitle); //string compare
if (searchresult ==0)
{
/* display details */
printf("\n Title Author ISBN ");
printf("\n %4i %-30s %s",num_records,record_array[num_records-1].Author,
record_array[num_records-1].Title);
}
screen_pause();
num_records++;
/* end of else */
/* end of while */
/* end of display_one_record */
}
/*******************************************************************************************/
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\tAuthor\t\tTitle");
num_records=0;
fread(&record_array[num_records],sizeof(record_structure),1,record_file);
while(!feof(record_file))
{
printf("\n%3i %18s %35s %55s ",num_records,record_array[num_records].ISBN,
record_array[num_records].Title,record_array[num_records].Author);
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();
}/* end of close function */
/*******************************************************************************************/
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 : option3(); break;
case 4 : display_all_records() ; break;
case 5 : break;
}
}/* end of menu function */
/*******************************************************************************************/
void screen_pause()
{
printf("\n\n\tPress any key to continue.....");
getch();
}/* end of screen_pause function */