Hi, may I know what is wrong with my codes?
SOmethhing went wrong when the first ISBN always cannot get through.DO i need to add an additional fflush()?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>
//initialisation
struct book_record
{
char ISBN[11];
char title[31];
char author[31];
char category[3];
char date[11];
float price;
};
struct BkArray
{
struct book_record BookArray[10];
};
//function prototypes
void add_records(BkArray *library);
void display_all_records(BkArray *library);
void display_information(char *ISBN, BkArray *library);
void display_books(BkArray *library);
void delete_records(char *title, BkArray *library);
void BarLine(int num);
BkArray library;
main()
{
// FILE *fp;
//variables
int choice;
char ISBN[11]; /*This ISBN refers to the input by the finder*/
char title[31];
char menu;
/* if((fp = fopen("test", "r+t") == NULL)
{
printf("\aError opening test\n"
exit(-1);
}
else
{
*/
BkArray library;
BkArray *lib;
for (int i = 0; i != 9; i++)
{
strcpy(library.BookArray.title, "NULL" );
strcpy(library.BookArray.author, "NULL" );
strcpy(library.BookArray.category, "NULL" );
strcpy(library.BookArray.date, "NULL" );
strcpy(library.BookArray.ISBN, "NULL" );
library.BookArray.price = 0.00;
}
do{
printf("\n"
BarLine(30);
printf("\tMENU\n"
BarLine(30);
printf("1 - Add new records.\n"
printf("2 - Display all book records.\n"
printf("3 - Find and display book information given the ISBN.\n"
printf("4 - Find all books written by a particular author.\n"
printf("5 - Delete book record given the title.\n"
printf("\nEnter your choice: "
scanf("%d", &choice);
lib = &library;
switch(choice)
{
case 1: add_records(lib);
break;
case 2: display_all_records(lib);
break;
case 3: printf("\nEnter ISBN: "
scanf("%s", ISBN);
display_information(ISBN, lib);
break;
case 4: display_books(lib);
break;
case 5: printf("\nEnter title: "
gets( title);
delete_records(title,lib);
break;
default: printf("\nThere is no such choice.\n"
}
printf("\nDo you want to go back to the menu? Press Y for yes."
scanf("%c", &menu);
}while(menu == 'Y'||'y');
// }
// fclose(fp);
return 0;
}
void BarLine(int num)
{
for(int l = 0; l < num; l++)
printf("="
printf("\n"
}//BarLine
//Add book records in Book Array
void add_records(BkArray* library)
{
//variables
int number;
printf("\nEnter the number of records that you are going to enter: "
scanf("%d", &number);
// clscr();
for( int k = 0; k < number; k++)
{
printf("\n#%d Record:\n", k+1);
BarLine(30);
printf("Enter ISBN: "
gets( library->BookArray[k].ISBN);
printf("Enter the title of the book: "
gets( library->BookArray[k].title);
printf("Enter the author of the book: "
gets(library->BookArray[k].author);
printf("Enter the price of the book: "
scanf("%.2f", &library->BookArray[k].price);
printf("Enter the category of the book: "
gets( library->BookArray[k].category);
printf("Enter the published date in the format of dd/mm/yyyy: "
gets (library->BookArray[k].date);
}//for
}//add_records
//Display all book records
void display_all_records(BkArray *library)
{
for (int p = 0; p < sizeof(library); p++)
{
printf("\n#%d Record:\n", p+1);
BarLine(30);
printf("ISBN: %s.\n", library->BookArray[p].ISBN);
printf("Title: %s.\n", library->BookArray[p].title);
printf("Author: %s.\n", library->BookArray[p].author);
printf("Price: %.2f.\n", library->BookArray[p].price);
printf("Category: %s.\n", library->BookArray[p].category);
printf("Published Date: %s.\n", library->BookArray[p].date);
}//for
}//display_records
// Find and display book information given the IBSN
void display_information(char *ISBN, BkArray *library)
{
int cnt = 0;
for (int q = 0; q < sizeof(library); q++)
{
if (strcmp(library->BookArray[q].ISBN,ISBN))
{
printf("Book Found!\n"
printf("ISBN: %s.\n", ISBN);
printf("Title: %s.\n", library->BookArray[q].title);
printf("Author: %s.\n", library->BookArray[q].author);
printf("Price: %.2f.\n", library->BookArray[q].price);
printf("Category: %s.\n", library->BookArray[q].category);
printf("Published Date: %s.\n", library->BookArray[q].date);
cnt++;
}//if
}//for
if(cnt == 0)
printf("Book cannot be found!"
}//display_information
//find all books written by a particular author
void display_books(BkArray *library)
{
//variable
char author1[31];
int cnt = 0;
printf("\nEnter author: "
gets(author1);
for(int r = 0; r < sizeof(library); r++)
{
if(strcmp(library->BookArray[r].author, author1))
{
printf("The title of the book is %s.\n", library->BookArray[r].title);
cnt++;
break;
}
}//for
if(cnt != 1)
printf("No Book is found!"
}//display_book
//Delete book record given the title
void delete_records(char *title, BkArray *library)
{
for (int i = 0; i < sizeof(library->BookArray); i++)
{
if(title == library->BookArray.title)
{
for (int j = i; j < sizeof(library->BookArray); j++)
strcpy(library->BookArray[j].title, library->BookArray[j++].title);
}//if
}//for
}//delete_record
SOmethhing went wrong when the first ISBN always cannot get through.DO i need to add an additional fflush()?
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <float.h>
//initialisation
struct book_record
{
char ISBN[11];
char title[31];
char author[31];
char category[3];
char date[11];
float price;
};
struct BkArray
{
struct book_record BookArray[10];
};
//function prototypes
void add_records(BkArray *library);
void display_all_records(BkArray *library);
void display_information(char *ISBN, BkArray *library);
void display_books(BkArray *library);
void delete_records(char *title, BkArray *library);
void BarLine(int num);
BkArray library;
main()
{
// FILE *fp;
//variables
int choice;
char ISBN[11]; /*This ISBN refers to the input by the finder*/
char title[31];
char menu;
/* if((fp = fopen("test", "r+t") == NULL)
{
printf("\aError opening test\n"
exit(-1);
}
else
{
*/
BkArray library;
BkArray *lib;
for (int i = 0; i != 9; i++)
{
strcpy(library.BookArray.title, "NULL" );
strcpy(library.BookArray.author, "NULL" );
strcpy(library.BookArray.category, "NULL" );
strcpy(library.BookArray.date, "NULL" );
strcpy(library.BookArray.ISBN, "NULL" );
library.BookArray.price = 0.00;
}
do{
printf("\n"
BarLine(30);
printf("\tMENU\n"
BarLine(30);
printf("1 - Add new records.\n"
printf("2 - Display all book records.\n"
printf("3 - Find and display book information given the ISBN.\n"
printf("4 - Find all books written by a particular author.\n"
printf("5 - Delete book record given the title.\n"
printf("\nEnter your choice: "
scanf("%d", &choice);
lib = &library;
switch(choice)
{
case 1: add_records(lib);
break;
case 2: display_all_records(lib);
break;
case 3: printf("\nEnter ISBN: "
scanf("%s", ISBN);
display_information(ISBN, lib);
break;
case 4: display_books(lib);
break;
case 5: printf("\nEnter title: "
gets( title);
delete_records(title,lib);
break;
default: printf("\nThere is no such choice.\n"
}
printf("\nDo you want to go back to the menu? Press Y for yes."
scanf("%c", &menu);
}while(menu == 'Y'||'y');
// }
// fclose(fp);
return 0;
}
void BarLine(int num)
{
for(int l = 0; l < num; l++)
printf("="
printf("\n"
}//BarLine
//Add book records in Book Array
void add_records(BkArray* library)
{
//variables
int number;
printf("\nEnter the number of records that you are going to enter: "
scanf("%d", &number);
// clscr();
for( int k = 0; k < number; k++)
{
printf("\n#%d Record:\n", k+1);
BarLine(30);
printf("Enter ISBN: "
gets( library->BookArray[k].ISBN);
printf("Enter the title of the book: "
gets( library->BookArray[k].title);
printf("Enter the author of the book: "
gets(library->BookArray[k].author);
printf("Enter the price of the book: "
scanf("%.2f", &library->BookArray[k].price);
printf("Enter the category of the book: "
gets( library->BookArray[k].category);
printf("Enter the published date in the format of dd/mm/yyyy: "
gets (library->BookArray[k].date);
}//for
}//add_records
//Display all book records
void display_all_records(BkArray *library)
{
for (int p = 0; p < sizeof(library); p++)
{
printf("\n#%d Record:\n", p+1);
BarLine(30);
printf("ISBN: %s.\n", library->BookArray[p].ISBN);
printf("Title: %s.\n", library->BookArray[p].title);
printf("Author: %s.\n", library->BookArray[p].author);
printf("Price: %.2f.\n", library->BookArray[p].price);
printf("Category: %s.\n", library->BookArray[p].category);
printf("Published Date: %s.\n", library->BookArray[p].date);
}//for
}//display_records
// Find and display book information given the IBSN
void display_information(char *ISBN, BkArray *library)
{
int cnt = 0;
for (int q = 0; q < sizeof(library); q++)
{
if (strcmp(library->BookArray[q].ISBN,ISBN))
{
printf("Book Found!\n"
printf("ISBN: %s.\n", ISBN);
printf("Title: %s.\n", library->BookArray[q].title);
printf("Author: %s.\n", library->BookArray[q].author);
printf("Price: %.2f.\n", library->BookArray[q].price);
printf("Category: %s.\n", library->BookArray[q].category);
printf("Published Date: %s.\n", library->BookArray[q].date);
cnt++;
}//if
}//for
if(cnt == 0)
printf("Book cannot be found!"
}//display_information
//find all books written by a particular author
void display_books(BkArray *library)
{
//variable
char author1[31];
int cnt = 0;
printf("\nEnter author: "
gets(author1);
for(int r = 0; r < sizeof(library); r++)
{
if(strcmp(library->BookArray[r].author, author1))
{
printf("The title of the book is %s.\n", library->BookArray[r].title);
cnt++;
break;
}
}//for
if(cnt != 1)
printf("No Book is found!"
}//display_book
//Delete book record given the title
void delete_records(char *title, BkArray *library)
{
for (int i = 0; i < sizeof(library->BookArray); i++)
{
if(title == library->BookArray.title)
{
for (int j = i; j < sizeof(library->BookArray); j++)
strcpy(library->BookArray[j].title, library->BookArray[j++].title);
}//if
}//for
}//delete_record