Logo 
Search:

C Programming Forum

Ask Question   UnAnswered
Home » Forum » C Programming       RSS Feeds

error in my code

  Asked By: Mirlon    Date: Nov 18    Category: C Programming    Views: 892
  

good afternoon,everyone,im having a small problem with this i have writen to store books i am getting one error and i just cant find it here is the code

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<ctype.h>
typedef struct Book{int book_id;
char isbn[20];
char title[255];
char author[45];
char publisher[255];
int year_publish;
char edition[128];

}BOOK;
void displayMenu();
void defaultOption();
void add_new_book(void);
void update_book_details(void );
void delete_book(void);
void view_single_book(void);
void view_entire_library(void);


int main()
{
int opt,i;
BOOK book;
system("cls");
displayMenu();
scanf("%d",&opt);
while(opt != 6)
{

switch(opt)
{
case '1':
add_new_book();
break;
case 2:
update_book_details();
break;
case 3:
delete_book();
break;
case 4:
view_single_book();
break;
case 5:
view_entire_library();
break;
case 6:
defaultOption();
break;
}
}
displayMenu();
scanf("%d", &opt);

system("cls");
system ("pause");
return 0;
}
/***************************
This function displays the menu
***************************/
void displayMenu()
{
system("cls");
printf("Please select an option from the menu:\n\n");
printf("\n1:\tto add new book press (1)\n");
printf("\n2:\tto update book details press(2)\n");
printf("\n3:\tto Delete Book Press(3)\n");
printf("\n4:\tto view single book Press(4)\n");
printf("\n5:\tto view entire library Press(5)\n");
printf("\n6:\tto Exit Press(6)\n");
printf("\nOption: ");
}
/***************************
This function handles
default menu options
***************************/
void defaultOption()
{
system("cls");
printf("Option doesn't exist!\n\nPress any key to go back to menu...");
getchar();
getchar();
}

/***************************
This function is responsible for adding a new book
***************************/
void add_new_book()
{
FILE *fp;
BOOK book;
fp= fopen("BOOK.txt","a+t");
system("cls");
printf("\nENTER BOOK ID: ");
scanf("%d",&book.book_id);
printf("ENTER ISBN: ");
fflush(stdin);
scanf("%s",book.isbn);
printf("ENTER BOOK TITLE: ");
scanf("%s",book.isbn);
fflush(stdin);
printf("ENTER BOOK AUTHOR: ");
scanf("%s",book.author);
printf("ENTER BOOK PUBLISHER : ");
scanf("%S",book.publisher);
printf("ENTER YEAR PUBLISHED: ");
scanf("%d",&book.year_publish);
printf("ENTER BOOK EDITION: ");
fflush(stdin);
scanf("%s",book.edition);
fscanf(stdin,"BOOK-ID : %d |ISBN : %s |BOOK TITLE:%s|BOOK AUTHOR: %s|BOOK PUBLISHER:%s|YEAR PUBLISHED:%d|BOOK EDITION: %s",&book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
fprintf(fp,"BOOK-ID : %d |ISBN : %s |BOOK TITLE:%s|BOOK AUTHOR: %s|BOOK PUBLISHER:%s|YEAR PUBLISHED:%d|BOOK EDITION: %s",book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
fclose(fp);
}
/***************************
This function deals with
updating a book
***************************/
void update_book_details()
{
FILE *fp;
BOOK book;
int book_id ;
fp = fopen("BOOK.txt","r+");
system("cls");
if(fp==NULL)
{
exit (0);
}
printf("ENTER THE ID OF THE BOOK YO WISH TO UPDATE: ");
scanf("%d",&book_id);
fprint(fp,"%d",book.book_id);
system("pause");
fclose(fp);
}
/***************************
This function deals with
deleting a book
***************************/
void delete_book()

{
FILE *fp,*temp;
BOOK book;
int id,found=0;
char choice;
fp=fopen("BOOK.txt","rb");
temp=fopen("temp.txt","wb");

system("cls");
printf("\nENTER BOOK ID YOU WISH TO DELETE: ");
scanf("%d",&id);
fread(&book,sizeof(BOOK),1,fp);
while(feof(fp))
{
if(book.book_id==id)
{
printf( " %d %s %s %s %s-%d %s",book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
fwrite(&book,sizeof(book),1,temp);
found=1;
}
fread(&book,sizeof(book),1,fp);
}
fclose(fp);
fclose(temp);

if(found)
{
do{
fflush(stdin);
printf("\nTHIS OPERATION IS IRREVERSIBLE! ARE YOU SURE YOU WANT TO DELETE THIS BOOK(Y/N): ");
scanf("%c",&choice);
choice=toupper(choice);
if(choice=='Y')
{
system("del BOOK.txt");
system("rename temp.txt BOOK.txt");
printf("\nTHE BOOK DELETED SUCCESSFULLY");
}
else
{
if(choice=='N')
{
system("del temp.txt");
exit(0);
}
else
{
printf("INVALID ID.\n");
}
while(choice!='y'&&choice!='N')



system("pause");
}



/***************************
This function deals with
viewing a single book
***************************/
void view_single_book()
{
FILE *fp;
BOOK book;
int book_id,pos;
fp=fopen("BOOK.txt","r");
system("cls");
printf("ENTER BOOK ID YOU WISH TO VIEW: ");
scanf("%d",&book_id);
pos=book_id*sizeof(book);
fseek(fp,pos,SEEK_SET);
fscanf(fp," %d %s %s %s %s %d %s",&book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
printf("\n\n\tID : %d\n",book.book_id);
printf("\tTHE BOOK ISBN IS ISBN: %s\n",book.isbn);
printf("\tTHE BOOK TITLE IS : %s\n",book.title);
printf("\tTHE BOOK AUTHOR IS : %s\n",book.author);
printf("\tTHE BOOK PUBLISHER IS : %s\n",book.publisher);
printf("\tTHE YEAR PUBLISH IS: %d\n",book.year_publish);
printf("\tTHE BOOK EDITION IS: %s\n",book.edition);
system("pause");
fclose(fp);
}
/***************************
This function deals with
viewing the entire book library
***************************/
void view_entire_library()
{
FILE *fp;
BOOK book;
int all_lib,i;
fp=fopen("BOOK.txt","r");
system("cls");
fseek(fp,0,SEEK_END);
all_lib=ftell(fp)/sizeof(book);
printf("\nBOOK-ID :\tISBN :\t\tBOOK TITLE:\tBOOK AUTHOR:\tBOOK PUBLISHER:\tYEAR PUBLISHED:\tBOOK EDITION:\n\n");
pirintf("%d",all_lib);
for(i=1;i<=all_lib;i++)
{
fscanf(fp," -15%d -15%s %s -15 %s -15%s-15%d -15%s",&book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
fprintf(stdout, " -15%d -15%s-15 %s-15 %s -15%s-15%d -15%s",book.book_id,book.isbn,book.title,book.author,book.publisher,book.year_publish,book.edition);
}
system("pause");
fclose(fp);

}

Share: 

 

No Answers Found. Be the First, To Post Answer.

 
Didn't find what you were looking for? Find more on error in my code Or get search suggestion and latest updates.




Tagged: