Question: C program In my mobile C compiler (its on android app) It always skips on the Please enter book title and goes to Please enter
C program
In my mobile C compiler (its on android app)
It always skips on the "Please enter book title" and goes to "Please enter author's name". Is there anything wrong on my code?

***************************************************** *****************************************************
Here is the source code
#include
// function prototype void accept_data(); void display_data(); void edit_record(); void delete_record(); int choice; while(1) { // display menu printf(" --------Book Management System-------- "); printf("1) Add Record "); printf("2) Delete Record "); printf("3) Update Record "); printf("4) Display Record "); printf("5) Exit"); printf(" Enter your choice: "); scanf("%d",&choice);
// switch case to call the functions switch(choice) { case 1: accept_data(); break; case 2: delete_record(); break; case 3: edit_record(); break; case 4: display_data(); break; case 5: exit(1); } // ask if the user wants to continue printf(" Do you want to continue (Y/N) :"); fflush(stdin); ch = getchar(); // if ch ='y' then continue if(ch=='y' || ch=='Y') continue; // if ch == 'n' then break if(ch=='n' || ch =='N') break;
} return 0; } // function to accept data from user void accept_data(){ FILE *fp; struct Book book; int Count = 0; if((fp=fopen("count.dat","r+b"))== NULL) { fp = fopen("count.dat","w+b"); Count = 1; fwrite(&Count,sizeof(int),1,fp); fclose(fp); } else { fread(&Count,sizeof(int),1,fp); Count++; rewind(fp); fwrite(&Count,sizeof(int),1,fp); fclose(fp); } printf(" --------ADD RECORDS--------"); fp = fopen("info.dat","a+b"); fflush(stdin); printf(" Please enter book title : "); gets(book.Title); fflush(stdin); printf(" Please enter author's name: "); gets(book.Author); fflush(stdin); printf(" Please enter genre : "); gets(book.Genre); fflush(stdin); printf(" Please enter Publisher : "); gets(book.Publisher); printf(" Please enter number of pages : "); scanf("%d",&book.total_pages); // writing data to file fwrite(&book,sizeof(struct Book),1,fp); printf(" Book info added successfully... "); fclose(fp); }
void display_data() { // file pointer FILE *fp; struct Book book; int Count = 0; if((fp=fopen("count.dat","r+b"))==NULL) { printf(" --------RECORDS--------"); printf(" Number of records : 0 "); } else { fread(&Count,sizeof(int),1,fp); printf(" --------RECORDS--------"); printf(" Number of records : %d",Count); fclose(fp); fp = fopen("info.dat","r+b"); // read records till the end of the file while(fread(&book, sizeof(struct Book), 1, fp)) { // printing the details of the book printf(" "); printf(" Book Title : %s",book.Title); printf(" Author : %s",book.Author); printf(" Genre : %s",book.Genre); printf(" Publisher : "); puts(book.Publisher); printf(" Total Pages : %d",book.total_pages); } // closing the file fclose(fp); } } // function to edit the record void edit_record() { char title[50]; struct Book book; int flag = 0; FILE *fp; printf(" --------EDIT RECORDS--------"); // asking for the title of the book printf(" Enter book title to edit : "); fflush(stdin); gets(title); // opening the file in read mode fp = fopen("info.dat","rb+"); // while loop to read till the end of the file while(fread(&book, sizeof(struct Book), 1, fp)) { // comparing title if(strcmp(book.Title,title) == 0) { // if found, accepting new data fflush(stdin); printf(" Please enter book title : "); gets(book.Title); fflush(stdin); printf(" Please enter author's name: "); gets(book.Author); fflush(stdin); printf(" Please enter genre : "); gets(book.Genre); fflush(stdin); printf(" Please enter publisher : "); gets(book.Publisher); printf(" Please enter pages : "); scanf("%d",&book.total_pages); // adjusting the file pointer fseek(fp, - (long)sizeof(book), 1); // writing the data to the file fwrite (&book, sizeof(struct Book), 1, fp); flag =1; break; }
} // closing the file fclose (fp); // if flag is 1 if(flag==1) // print message printf(" Record updated successfully "); else printf(" Record not found "); } // function to delete the record void delete_record() { char title[50]; struct Book book; int flag = 0; FILE *fp,*fp2; printf(" --------DELETE RECORDS--------"); // accepting tile of the book printf(" Enter book title to edit : "); fflush(stdin); gets(title); // opening the file in read mode fp = fopen("info.dat","rb"); // opening temp file in write mode fp2 = fopen("temp.dat","wb"); // reading records till the end of the file while(fread(&book, sizeof(struct Book), 1, fp)) { // comparing title if(strcmp(book.Title,title) != 0) { // write all the records that does not match with the title to delete fwrite (&book, sizeof(struct Book), 1, fp2); } else if(strcmp(book.Title,title) == 0) { // make flag as 1 if title found flag = 1; } } // closing the file fclose (fp); fclose(fp2); // removing info.dat file remove("info.dat"); // renaming temp.dat as info.txt rename("temp.dat","info.dat"); if(flag==1) { // print the message int Count = 0; if((fp=fopen("count.dat","r+b"))== NULL) { fp = fopen("count.dat","w+b"); Count = 1; fwrite(&Count,sizeof(int),1,fp); fclose(fp); } else { fread(&Count,sizeof(int),1,fp); Count--; rewind(fp); fwrite(&Count,sizeof(int),1,fp); fclose(fp); } printf(" Record deleted successfully "); }
else printf(" Record not found "); }
1:48 Vill 83% test -Book Management System-- 1) Add Record 2) Delete Record 3) Update Record 4) Display Record 5) Exit Enter your choice: 1 --ADD RECORDS-- Please enter book title : Please enter author's name: # A * - = 11 2 + 1 V > : 3 X L + ! $ % & ? [ ] (). / { } 1 ; 1 2 3 4 5 6 7 8 9 0 q q we t u i o d f gh j k l X cvb n r N m tab space
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
