C: Problem with accessing struct variables

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
3 replies · 2K views
gruba
Messages
203
Reaction score
1

Homework Statement


I have problems with the following program which is long to post at once so I will explain what are the problems step by step.
2. The attempt at a solution
1. Define two structs that represents books and a bookstore as following:
Code:
typedef struct
{
 char id[14];
 char title[16];
 int availableNumOfBooksWithSameTitle;
}BOOK;

typedef struct
{
 char bookstoreName[31];
 int totalNumberOfBooksInBookstore;
 BOOK *books;
}BOOKSTORE;

2. Write a function that reads n books. If the book with previously read ID already exists, then just increment variable availableNumOfBooksWithSameTitle.
Code:
void readBook(BOOK *bk)
{
 printf("ID: ");
 scanf("%s",bk->id);
 while(strlen(bk->id) != 13)
 {
 printf("Invalid ID length. Try again.\n");
 printf("ID: ");
 scanf("%s",bk->id);
 }
 printf("Title: ");
 scanf("%s",bk->title);
 //Does this variable has to be read, or to keep track of books with the same title?
 printf("Available number of books with the same title: ");
 scanf("%d",&bk->availableNumOfBooksWithSameTitle);
}

BOOK* readNBooks(int *n)
{
 /*If the user enters ID which already exists, then
 increment the current number of availableNumOfBook.
 How to implement this?
 */
 do
 {
 printf("n = ");
 scanf("%d",n);
 }
 while(*n < 1);

 BOOK *arr;
 arr=(BOOK *)malloc(*n * sizeof(BOOK));
 int i;
 for(i=0; i<*n;i++)
 {
 printf("%d. book: \n",i+1);
 readBook(arr+i);
 }

 return arr;
}

3. Read data about a bookstore.
Code:
void readBookstore(BOOKSTORE *bs)
{
 //How to get the total num. of all books?
 printf("Bookstore name: ");
 scanf("%s",bs->bookstoreName);
 printf("Total number of books in bookstore: ");

 bs->books=calloc(bs->totalNumberOfBooksInBookstore,sizeof(*bs->books));
 int i;
 for(i=0; /*i < numOfBooksWithDifferentTitle*/ ; i++)
 {
 printf("%d. book:\n",i+1);
 readBook(bs->books+i);
 }
}

4. Sort data about books in descending order by ID.
Code:
void sortBooks(BOOKSTORE *b)
{
 //How to get the number of books with different title?
 int i,j;
 for(i=0; /*i < numOfBooksWithDifferentTitle-1*/ ; i++)
 for(j=i+1; /*j < numOfBooksWithDifferentTitle*/ ; j++)
 if(strcmp(b->books[i].id,b->books[j].id) < 0)
 {
 BOOK temp=b->books[i];
 b->books[i]=b->books[j];
 b->books[j]=temp;
 }
}

5. Allow user to purchase the book from a bookstore if it is available.
Code:
void purchase(BOOK *bk,char *bookTitle)
{
 //How to check if book with specified title is available?
 if(/*bookExists()*/ && bk->availableNumOfBooksWithSameTitle > 0)
 {
 bk->availableNumOfBooksWithSameTitle--;
 printf("Book %s is sold.",bk->title);
 }
 else
 printf("Book is not available.");
}

6. Print formatted data about a bookstore.
Code:
void printBookstore(BOOKSTORE *b)
{
 //How to get the number of books with the same
 //and with different title?
 printf("BOOKSTORE:%s",b->bookstoreName);
 printf("BOOKS:\n");
 printf("NUM BOOK ID  BOOK TITLE  NUM. OF BOOKS WITH THE SAME TITLE\n");
 printf("--- ------------- --------------- ---------------------------------\n");
 int i;
 for(i=0; /*i < numOfBooksWithDifferentTitle*/; i++)
 {
 printf("%2d.",i+1);
 printf("%-13s %-15s %d",b->books[i].id,b->books[i].title,/*numOfBooksWithSameTitle*/);
 printf("\n");
 }
 printf("--- ------------- --------------- ---------------------------------");
}

6. main()
Code:
int main()
{
 BOOKSTORE *bs;
 BOOK bk;
 char *bookTitle
 readBookstore(bs);
 sortBooks(bs);
 purchase(&bk,bookTitle);
 printBookstore(bs);
 free(bs);

 return 0;
}

If someone could reply I would be very thankful.

Full program:
Code:
#include <stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct
{
  char id[14];
  char title[16];
  int availableNumOfBooksWithSameTitle;
}BOOK;

typedef struct
{
  char bookstoreName[31];
  int totalNumberOfBooksInBookstore;
  BOOK *books;
}BOOKSTORE;

void readBook(BOOK *bk);//reads one book
BOOK* readNBooks(int *n);//reads n number of books
void readBookstore(BOOKSTORE *bs);//reads bookstore with books
void sortBooks(BOOKSTORE *b);//sorts books by descending order of ID
void purchase(BOOK *bk,char *bookTitle);//prints message about successful or unsuccessful purchase
void printBookstore(BOOKSTORE *b);//prints formatted data about bookstore

void readBook(BOOK *bk)
{
  printf("ID: ");
  scanf("%s",bk->id);
  while(strlen(bk->id) != 13)
  {
  printf("Invalid ID length. Try again.\n");
  printf("ID: ");
  scanf("%s",bk->id);
  }
  printf("Title: ");
  scanf("%s",bk->title);
   
  //Does this variable has to be read, or to keep track of books with the same title?
  printf("Available number of books with the same title: ");
  scanf("%d",&bk->availableNumOfBooksWithSameTitle);
}

BOOK* readNBooks(int *n)
{
  /*If the user enters ID which already exists, then
  increment the current number of availableNumOfBook.
  How to implement this?
  */
   
  do
  {
  printf("n = ");
  scanf("%d",n);
  }
  while(*n < 1);

  BOOK *arr;
  arr=(BOOK *)malloc(*n * sizeof(BOOK));
  int i;
  for(i=0; i<*n;i++)
  {
  printf("%d. book: \n",i+1);
  readBook(arr+i);
  }

  return arr;
}

void readBookstore(BOOKSTORE *bs)
{
  //How to get the total num. of all books?
   
  printf("Bookstore name: ");
  scanf("%s",bs->bookstoreName);
  printf("Total number of books in bookstore: ");

  bs->books=calloc(bs->totalNumberOfBooksInBookstore,sizeof(*bs->books));
  int i;
  for(i=0; /*i < numOfBooksWithDifferentTitle*/ ; i++)
  {
  printf("%d. book:\n",i+1);
  readBook(bs->books+i);
  }
}

void sortBooks(BOOKSTORE *b)
{
  //How to get the number of books with different title?
   
  int i,j;
  for(i=0; /*i < numOfBooksWithDifferentTitle-1*/ ; i++)
  for(j=i+1; /*j < numOfBooksWithDifferentTitle*/ ; j++)
  if(strcmp(b->books[i].id,b->books[j].id) < 0)
  {
  BOOK temp=b->books[i];
  b->books[i]=b->books[j];
  b->books[j]=temp;
  }
}

void purchase(BOOK *bk,char *bookTitle)
{
  //How to check if book with specified title is available?
   
  if(/*bookExists()*/ && bk->availableNumOfBooksWithSameTitle > 0)
  {
  bk->availableNumOfBooksWithSameTitle--;
  printf("Book %s is sold.",bk->title);
  }
  else
  printf("Book is not available.");
}

void printBookstore(BOOKSTORE *b)
{
  //How to get the number of books with the same
  //and with different title?
   
  printf("BOOKSTORE:%s",b->bookstoreName);
  printf("BOOKS:\n");
  printf("NUM BOOK ID  BOOK TITLE  NUM. OF BOOKS WITH THE SAME TITLE\n");
  printf("--- ------------- --------------- ---------------------------------\n");
  int i;
  for(i=0; /*i < numOfBooksWithDifferentTitle*/; i++)
  {
  printf("%2d.",i+1);
  printf("%-13s %-15s %d",b->books[i].id,b->books[i].title,/*numOfBooksWithSameTitle*/);
  printf("\n");
  }
  printf("--- ------------- --------------- ---------------------------------");
}

int main()
{
  BOOKSTORE *bs;
  BOOK bk;
  char *bookTitle
   
  readBookstore(bs);
  sortBooks(bs);
  purchase(&bk,bookTitle);
  printBookstore(bs);
  free(bs);

  return 0;
}
 
on Phys.org
DrClaude said:
You haven't explained what the problem is.
I have commented everything in the code.
 
gruba said:
I have commented everything in the code.
Not good enough. If you want us to help you, make at as convenient as you can for us. Tell us which function is not working how it should, with information about the inputs to the function you're using, specific compiler errors (if you have syntax errors), specific run-time errors (if applicable), which variables are not being set or are being set with incorrect values, and any other information that might be helpful.

We're doing you a favor by helping, so do us a favor by not making us pore through all of you code and trying to guess what the problem might be.