C: Problem with accessing struct variables

Click For Summary

Discussion Overview

The discussion revolves around a programming problem related to accessing and managing struct variables in C, specifically in the context of a bookstore application. Participants are addressing issues with reading book data, managing book availability, sorting books, and handling user purchases.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes a program that defines structs for books and bookstores, outlining functions for reading book data, sorting, and purchasing.
  • There are questions about how to handle existing book IDs and whether to increment the count of available books with the same title.
  • Another participant requests clarification on the specific problems encountered, emphasizing the need for detailed information about errors or unexpected behavior.
  • Further comments stress the importance of providing context, such as inputs, outputs, and specific errors, to facilitate assistance.

Areas of Agreement / Disagreement

Participants generally agree that the original poster needs to provide more specific information about the problems they are facing. There is no consensus on the specific issues within the code, as the original poster has not clearly articulated them.

Contextual Notes

Participants note the lack of clarity regarding which functions are malfunctioning, what inputs are being used, and any compiler or runtime errors that may be occurring. This ambiguity limits the ability to provide targeted help.

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
2K
  • · 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
3K
  • · Replies 12 ·
Replies
12
Views
3K
Replies
7
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K