1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

C: Problem with accessing struct variables

  1. Mar 1, 2016 #1
    1. The problem statement, all variables and given/known data
    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 (Text):

    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 (Text):

    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 (Text):

    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 (Text):

    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 (Text):

    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 (Text):

    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 (Text):

    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 (Text):

    #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;
    }
     
     
  2. jcsd
  3. Mar 1, 2016 #2

    DrClaude

    User Avatar

    Staff: Mentor

    You haven't explained what the problem is.
     
  4. Mar 1, 2016 #3
    I have commented everything in the code.
     
  5. Mar 1, 2016 #4

    Mark44

    Staff: Mentor

    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.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted



Similar Discussions: C: Problem with accessing struct variables
Loading...