C: Problem with accessing struct variables

Click For Summary
The discussion revolves around issues with a C program designed to manage a bookstore's inventory. Key problems include how to handle duplicate book IDs by incrementing the count of available copies, determining the total number of books for allocation, and sorting books by ID. Participants emphasize the need for clearer problem statements, including specific errors or unexpected behaviors encountered during execution. The importance of providing detailed information about function inputs and outputs is highlighted to facilitate effective troubleshooting. Overall, the conversation stresses the necessity of clarity and specificity in coding queries for better assistance.
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;
}
 
Physics news on Phys.org
You haven't explained what the problem is.
 
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.
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
11K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 17 ·
Replies
17
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K