Hi guys this program needs to have two options, one where the user enters an ISBN number and then it displays the book which has the ISBN number the user entered and the second option is the exact same except the user enters a title instead. My teacher says i should use a string compare function but i honestly have no idea how to do this can someone please point me in the right direction.
This is my program so far...
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;
/* function prototypes */
void intro();
void menu();
void close();
void add_book();
void search_for_isbn();
void search_for_title();
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<=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();
}/* end of dummy menu option 1 function */
/*******************************************************************************************/
void search_for_isbn()
{
system("cls");
printf("Search for ISBN ");
screen_pause();
}/* end of dummy menu option 2 function */
/*******************************************************************************************/
void search_for_title()
{
system("cls");
printf("Search for a book by title ");
screen_pause();
}/* end of dummy menu option 3 function */
/*******************************************************************************************/
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 : search_for_isbn(); break;
case 3 : search_for_title(); 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 */