Following is the code of a library management system using data structures. the first function is to add a book. and the second is to display all the books. I am able to input the data , and also return the data from addBookInfo() function. But when sending the data( in case 2 of switch statement) it is giving price as 00.000. Please help me to correct the mistake.
#include<stdio.h>
#include<string.h>
#include<stdbool.h>
#include<stdlib.h>
#define MAXCHAR 100
typedef struct library
{
char bookTitle[MAXCHAR];
char authorName[MAXCHAR];
float price;
bool availability;
}library;
void addBookInfo(library**book);
void displayBookInfo(library**);
//void authorName();
//void displayTitle();
//void numberOfBooks();
int count;
int main()
{
int num,i;
int select;
library **book=(library**)malloc(sizeof(library));
// library** temp;
// library *Head=(library*)malloc(sizeof(library));
// *book = Head;
while(1)
{
printf ("\n1. Add book info.\n 2.Display Book Info\n 3.List all the books of a given author \n 4.List the title of the given book\n");
////////////// printf("5. List the count of the books in the library \ņ 7. Exit\n");
printf("Select an option\n");
scanf("%d",&select);
switch (select)
{
case 1 :addBookInfo(book);
printf("%f...",book[0]->price); //Works price getting displayed
break;
case 2 :
//
printf("++++%f++++",book[0]->price); //not working price showing as 00.000
displayBookInfo(book);
// printf("++++%f++++",book[0]->price);
break;
/*
case 3 :
break;
case 4 :
break;
case 5 :
break;
case 6 :
break;
*/
case 7 :printf(" EXITING...");
exit(0);
break;
default : printf(" invalid number \n ");
}
}
return 0;
}
void addBookInfo(library **book )
{
library books;
//library **book=(library**)malloc(sizeof(library));
printf("Enter Book details...\n");
printf("Enter Book name \n");
scanf("%s",books.bookTitle);
printf("Enter author name\n");
scanf("%s",books.authorName);
printf("Enter the price \n");
scanf("%f",&books.price);
books.availability=true;
*book=&books;
*book++;
count++;
// printf("%f",book[0]->price);
// return book;
}
void displayBookInfo(library**book)
{
int i;
printf("Display info of all the books\n");
for(i=0;i<count;i++)
{
printf("%f\n,",book[0]->price);// not working price showing as 00.000 or segmentation fault
}
}
Lots of problems.
1) You want to create an array of library into main function, so you should have, at least:
#define BOOK_ARRAY_ELEMS 100
library **book= malloc(sizeof(library*)*BOOK_ARRAY_ELEMS);
if (book == NULL)
{
exit(1);
}
2) books
has local scope inside addBookInfo
, so is stack allocated. You cannot export stack allocated var address. You can use malloc to create that kind of variable.
3) You are using a global value count
to count inserted book, so you can use it to point the correct element of your array.
void addBookInfo(library **book )
{
library *books = malloc(sizeof(library));
if (books != NULL)
{
printf("Enter Book details...\n");
printf("Enter Book name \n");
scanf("%s", &books->bookTitle[0]);
printf("Enter author name\n");
scanf("%s", &books->authorName[0]);
printf("Enter the price \n");
scanf("%f",&books->price);
books->availability=true;
book[count]=books;
count++;
}
else
{
fprintf(stderr, "Error allocating book\n");
exit(1);
}
}
void displayBookInfo(library**book)
{
int i;
printf("Display info of all the books\n");
for(i=0;i<count;i++)
{
printf("%f,", book[i]->price);
}
printf("\n");
}
Don't forget to free all malloc
ated variables, so case 7
case 7 :
{
printf(" EXITING...");
for (i=0; i<count; i++)
{
free(book[i]);
}
free(book);
exit(0);
}
break;
BTW I guess you are trying to achieve a concatenated list, that is another long story....
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.