Question: Open Code::Blocks and start a new console application project. In your main.c , define a structure called book (follow the bank_record example) that contains: char

  1. Open Code::Blocks and start a new console application project.
  2. In your main.c, define a structure called book (follow the bank_record example) that contains:
    1. char title[255];
    2. char author_name[50];
    3. char ISBN[10];
    4. int pages;
    5. int year_published;
  1. In your main ( ), create a 360-element array of book-structures. These will be populated in a later step.
    1. Example single instance of a blank structure: book my_book;
    2. Example array of blank structures: book my_book_array [50];
  2. Create a function called parse_file ( ) that takes a parameter of filename and an array of book structures to populate. Call this function in your main ( ) to populate your array of books.
    1. This function should open the desired file and loop through each line, scanning book information and populating new instances of the book structure array.
      1. Each line holds one book record, separated by commas.
      2. Consider using the strtok ( ) function from the library to tokenize (parse) a line by a comma delimiter.
      3. Line format: Book Title, Author Name, ISBN, Number of pages, Year published
    2. NOTE: If you followed the typedef struct format described in the introduction of this lab, passing a structure array to a function is accomplished the same way as passing any other type of array to functions.
    3. NOTE: Some records may be missing information, such as ISBN or page numbers. They will be N/A in the BookList.csv, make sure to handle that string where your program may be expecting an integer.
    4. NOTE: A skeleton of this function has been provided to you in Appendix-B of this lab handout.
  3. Create a function called print_book ( ) that takes a book as a parameter. The function should neatly print the contents of that books structure to the terminal window. Remember: some records might have null parameters, make sure to handle that in your output.
  4. Create a function called search_title ( ) that takes a book array, the number of books in that book array, and a character string title as parameters. This search function should loop through all of the books in the array in search of a title that matches with the passed character string, printing to the terminal any books that fulfill this (there could be more than 1 match).
  5. Create a function called search_author ( ) that takes a book array, the number of books in that book array, and a character string author as parameters. This search function should loop through all of the books in the array in search of an author (first and last name) that matches with the passed character string, printing to the terminal any books that fulfill this (there could be more than 1 match).
  6. Create a function called search_ISBN ( ) that takes a book array, the number of books in that book array, and an character string ISBN as parameters. This search function should loop through all of the books in the array in search of an ISBN that matches with the ISBN passed, printing to the terminal any books that fulfill this (there could be more than 1 match).
  7. In your main(), create a while-loop that monitors user input for an integer. The integer input will act as a search by selection to offer the following search features:
    1. [0] Search by Title
    2. [1] Search by Author Name
    3. [2] Search by ISBN

Here is a list of Book for a reference

You Might Be a Zombie and Other Bad News,Cracked.com,452296390,295,2011 The Black Company (The Chronicles of the Black Company #1),Glen Cook,N/A,319,1992 Gardens of the Moon (Malazan Book of the Fallen #1),Steven Erikson,765348780,666,2005 Educating Engineers: Theory Practice and Imagination (Jb-Carnegie Foundation for the Adavancement of Teaching),Kelly Macatangay,787977438,280,2008 The Sworn Sword (The Tales of Dunk and Egg #2),George R.R. Martin,N/A,81,2011 The Mystery Knight (The Tales of Dunk and Egg #3),George R.R. Martin,N/A,104,2010 The Female Eunuch,Germaine Greer,374527628,400,2002

This is my code and I need help with coding correctly. It gets to the point of searching and it won't find it from the list.

#include #include #include /* book structure */ struct book_struct{ char title[255]; char auther_name[50]; char ISBN[10]; int pages; int year_published; };

/* read file , and store in array */

int parse_file(char fileName[30],struct book_struct *books){ FILE *fp; char line[350]; int n=0; char title[255]; char auther_name[50]; char ISBN[10]; int pages; int year_published; char *token; int i=0; fp=fopen(fileName,"r");

if(fp!=NULL){

/* read each line */ while(fgets(line, 350, fp) != NULL){ i=0; token = strtok(line, ",");

/* walk through other tokens */ while( token != NULL ) { /* store corresponding token in structure array */ if(i==0){ strcpy(books[n].title,token); } if(i==1){ strcpy(books[n].auther_name,token); } if(i==2){ strcpy(books[n].ISBN,token); } if(i==3){ books[n].pages=atoi(token); } if(i==4){ books[n].year_published=atoi(token); }

token = strtok(NULL, ",");

/* increase token index */ i++; } /* increse number of line */ n++; } }

fclose(fp); return n; }

/* print all books */ void print_book(struct book_struct *books,int n){ int i; for(i=0;i

/* search book with title */ void search_title(struct book_struct *books,int n,char *title){ int i; for(i=0;i

/* search book with auther name */

void search_auther(struct book_struct *books,int n,char *auther_name){ int i; for(i=0;i

/* search book with ISBN number */

void search_ISBN(struct book_struct *books,int n,char *isbn){ int i; for(i=0;i

void main(){ char fileName[30]; int n; int option; struct book_struct my_book_array[50]; char title[255]; char auther_name[50]; char isbn[10];

/* read file name */ printf("Enter filename : "); scanf("%s",&fileName);

/* load file data to structure array */ n=parse_file(fileName,my_book_array);

/* print books */ printf("--- BooK List --- "); print_book(my_book_array,n);

while(1){ /* menu option */ printf("[0] Search by Title "); printf("[1] Search by Auther Name "); printf("[2] Search by ISBN "); printf("[3] Exit "); printf("Choose option : ");

scanf("%d",&option);

/* serach and print book list */ if(option==0){

fflush(stdin); printf("Enter title : "); fgets(title,256,stdin); title[strlen(title)-1]='\0'; search_title(my_book_array,n,title); } if(option==1){

fflush(stdin); printf("Enter auther name : "); fgets(auther_name,50,stdin); auther_name[strlen(auther_name)-1]='\0'; search_auther(my_book_array,n,auther_name); } if(option==2){

fflush(stdin); printf("Enter ISBN : "); fgets(isbn,10,stdin); isbn[strlen(isbn)-1]='\0'; search_ISBN(my_book_array,n,isbn); }

if(option==3){ break; }

if(option!=0 && option!=1 && option!=2 && option!=3){ printf(" Please choose correct option "); } }

printf(" End Program");

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!