C: Problem with accessing struct variables

In summary, the conversation is about a program that is not working and the person is asking for help. They have provided the code and commented on it, but have not specified the exact problem or given enough information for others to help them effectively. They are asked to provide more specific information and make it easier for others to help them.
  • #1
gruba
206
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
  • #2
You haven't explained what the problem is.
 
  • #3
DrClaude said:
You haven't explained what the problem is.
I have commented everything in the code.
 
  • #4
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.
 

FAQ: C: Problem with accessing struct variables

How do I access variables inside a struct in C?

To access variables inside a struct in C, you can use the dot operator (.) followed by the variable name. For example, if your struct is named "person" and has a variable "name", you can access it by typing "person.name".

Why am I getting an error when trying to access struct variables in my C program?

There could be several reasons for this error. One common reason is that you have not properly declared or initialized your struct. Make sure that you have declared your struct before trying to access its variables, and that you have properly assigned values to those variables.

Can I access struct variables using pointers in C?

Yes, you can access struct variables using pointers in C. Pointers allow you to directly access the memory address of a struct variable, which can be useful in certain situations. To access a struct variable using a pointer, you can use the arrow operator (->) followed by the variable name. For example, if you have a pointer to a struct named "personPtr" and want to access the "age" variable, you can type "personPtr->age".

How can I access variables inside a nested struct in C?

To access variables inside a nested struct in C, you can use multiple dot operators. For example, if you have a struct named "person" which contains another struct named "address", and you want to access the "city" variable inside the "address" struct, you can type "person.address.city".

Is it possible to change the value of a struct variable in C?

Yes, it is possible to change the value of a struct variable in C. You can do this by directly assigning a new value to the variable using the assignment operator (=). For example, if you want to change the value of the "age" variable in a struct named "person", you can type "person.age = 30".

Similar threads

Replies
3
Views
1K
Replies
3
Views
1K
Replies
1
Views
9K
Replies
4
Views
1K
Replies
4
Views
2K
Replies
3
Views
973
Replies
12
Views
2K
Replies
4
Views
2K
Back
Top