In my class i been assigned to make a Library menu program which includes
a structure with bookname/author/price/issue date; then a menu with different options like addbook,display, search by author etc
i believe i got the basic idea of how to do it and i made a draft of it in codeblocks. but problem is code working but i cant get input/output of book name only (rest working i think) and i cant find the reason why. im a beginner in programming just been doing it for 2 month or so, so i would be very greatful if someone can point out whats wrong in my code.
Thanks in advance and here is my code>
#include<stdio.h>
#include<string.h>
int bookcount=0;
struct library{
char name[100];
char author[100];
float price;
char date[20];
}str[100];
int main(){
int i;
menu();
return 0;
}
int menu(){
int choice,i;
for(i=0;;i++){
printf("\nWelcome to library menu.Please enter a choice from below-");
printf("\n\nPress 1 to add book information\nPress 2 to Display book information\nPress 3 to search books by author name");
printf("\nPress 4 to list the count of books\nPress 5 to find all the books for given price\nPress 0 for exit\nChoice=");
scanf("%d",&choice);
if(choice==0)
break;
else{
switch(choice){
case 1:
addbook();
break;
case 2:
display();
break;
case 3:
searchbyauthor();
break;
case 4:
listcount();
break;
case 5:
findbyprice();
break;
default:
printf("invalid input!\n");
}
}
}
return 0;
}
int addbook(){
int n,i;
printf("\nEnter number of books to add=");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("\nEnter book title=");
gets(str[bookcount].name);
printf("\nEnter book author=");
gets(str[bookcount].author);
printf("\nEnter book date=");
gets(str[bookcount].date);
printf("\nEnter book price=");
scanf("%f",&str[bookcount].price);
bookcount++;
}
return 0;
}
int display(){
int i;
for(i=0;i<bookcount;i++){
printf("\nBook no %d name=",i+1);
puts(str[i].name);
printf("\nBook no %d author=",i+1);
puts(str[i].author);
printf("\nBook no %d issue date=",i+1);
puts(str[i].date);
printf("\nBook no %d price=%f",i+1,str[i].price);
}
return 0;
}
int searchbyauthor(){
char inp[100];
int i;
printf("\nEnter Author name to search=");
gets(inp);
for(i=0;i<bookcount;i++){
if(strcmp(str[i].author,inp)==0)
printf("\nBook name=%s",str[i].name);
}
return 0;
}
int listcount(){
printf("\nnumber of books are =%d\n",bookcount);
return 0;
}
int findbyprice(){
float inp;
int i;
printf("\nEnter price to search=");
scanf("%f",&inp);
for(i=0;i<bookcount;i++){
if(str[i].price==inp)
printf("\nBook name=%s",str[i].name);
}
return 0;
}