Here's my code so far:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int choice;
struct bookType
{
char title[30];
char isbn[13];
char author[30];
};
int search()
{
struct bookType book;
int i = 0;
char name[80];
getchar();
printf("Enter a book title to search: ");
fgets(name, 80, stdin);
FILE *BookFile = fopen("BookFile.bin", "rb+");
rewind(BookFile);
int counter;
while(counter == 0)
{
counter = fread(&book, sizeof(book), 1, BookFile);
if(strcmp(book.title, name) == 0)
{
printf("\nBook found, yey! \nThis is the book's title: %s", book.title);
printf("\nThis is the book's author: %s", book.author);
printf("\nThis is the book's ISBN: %s", book.isbn);
i = 1;
}
else if(counter != 0)
{
printf("\nBook not found :(/>\n");
i = 1;
}
}
return 0;
}
void addBook()
{
struct bookType book;
int i;
i = 0;
do
{
printf("Welcome to the add book section of this software.\n");
printf("\nPlease enter the name of the book: ");
getchar();
fgets(book.title, 30, stdin);
printf("\nPlease enter the book's ISBN number: ");
fgets(book.isbn, 15, stdin);
printf("\nPlease enter the name of the book's author: ");
fgets(book.author, 30, stdin);
FILE *BookFile = fopen("BookFile.bin", "ab+");
if(!BookFile)
{
printf("Error opening BinFile.bin");
}
else
{
fwrite(&book, sizeof(book), sizeof(book), BookFile);
fclose(BookFile);
FILE *BookFile = fopen("BookFile.bin", "rb+");
fread(&book, sizeof(BookFile), 3, BookFile);
printf("\nThis is the name of the book: %s", book.title);
printf("\nThis is the isbn number: %s", book.isbn);
printf("\nThis is the author's name: %s", book.author);
fclose(BookFile);
}
char writeAnother;
int writeAnotherCounter = 0;
do
{
printf("\nWould you like to write another record? (y/n) ");
scanf("%s", &writeAnother);
switch(writeAnother)
{
case 'y':
i = 0;
writeAnotherCounter = 1;
break;
case 'n':
i = 1;
writeAnotherCounter = 1;
break;
default :
printf("Please enter a valid condition! \n");
break;
}
}while(writeAnotherCounter != 1);
}while(i != 1);
}
int main(int argc, char *argv[])
{
printf("Welcome to the library system developed by Conner McCabe. \n");
printf("-----------------------------------------------------------\n");
printf("* Please make a selection from one of the options below: *\n");
printf("-----------------------------------------------------------\n");
printf("* *\n");
printf("* 1. Search for a Book *\n");
printf("* *\n");
printf("* 2. Add a Book to the Library *\n");
printf("* *\n");
printf("* 3. Exit the Program *\n");
printf("* *\n");
printf("* *\n");
printf("-----------------------------------------------------------\n");
do
{
printf("\nPlease enter a number from 1 - 3: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
search();
break;
case 2:
addBook();
break;
case 3:
exit(0);
break;
default :
printf("Please enter a valid condition! \n");
break;
}
}while(choice != 1 || choice != 2 || choice != 3 );
system("sleep");
return 0;
}

New Topic/Question
Reply




MultiQuote





|