Home > Mobile >  Problems on declaring char - undeclared (first use in this function)
Problems on declaring char - undeclared (first use in this function)

Time:12-22

I'm trying to code build and run the program but this error message came out :

'MAX_BOOK_NAME' undeclared (first use in this function)

Another issue I'm facing is for the program to require and implement the following: You must use the following struct to store information about one book:

struct book {
char *title;
char *author;
char *subject;
};

You must use the following struct to store information about the library collection:

struct library {
struct book collection;
int num_books;
struct library *next;
};

The only function you are specifically required to write is a copy function, which copies the contents of one book into another book. Here is the prototype for that function:

void copybook(struct book* dest, struct book* source);

Though the rest of the function prototypes will not be given, it is expected that you follow good programming design and create several functions with well-specified tasks related to the solution of this problem. Make sure to pay very careful attention to parameter passing. In particular, in each function that needs a variable of type struct library, make sure to pass the variable by reference, as follows:

void addBook(struct library* thislib);

This will ensure that any change made to the library in the function is reflected in main. Inside a function like this one, remember to access either component, use the following expressions:

thislib->collection
thislib->num_books

Whenever you add a book to the collection, make sure you add it to the end of the collection. Do not forget to update the variable num_books in the struct library variable. Whenever you delete a book from the collection, make sure you copy the book in the second-last slot into the vacated spot. For example, if the book to be deleted is in position 3 and the number of books in the collection before deleting is 7, then the book in position 6 (the second-last filled position) should be moved to the book in index 3. Subsequently, the number of books in the library should be updated to hold only 6.

Below is what I had done so far,

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


void viewBooks()
{
    int found = 0;

    char bookName[MAX_BOOK_NAME] = {0};

{
    s_BooksInfo_addBookInfoInDataBase = {0};

    FILE *fp = NULL;

    int status = 0;

    unsigned int countBook = 1;

    headMessage("VIEW BOOKS DETAILS");

    fp = fopen(FILE_NAME,"rb");

    if(fp == NULL)
    {
        printf("File is not opened\n");

        exit(1);
    }

    if (fseek(fp,FILE_HEADER_SIZE,SEEK_SET) != 0)
    {
        fclose(fp);

        printf("Facing issue while reading file\n");

        exit(1);
    }

    while (fread (&addBookInfoInDataBase, sizeof(addBookInfoInDataBase), 1, fp))
    {
        printf("\n\t\t\tBook Count = %d\n\n",countBook);

        printf("\t\t\tBook id = %u",addBookInfoInDataBase.books_id);

        printf("\n\t\t\tBook name = %s",addBookInfoInDataBase.bookName);

        printf("\t\t\tBook authorName = %s",addBookInfoInDataBase.authorName);

        printf("\t\t\tBook issue date(day/month/year) = (%d/%d/%d)",addBookInfoInDataBase.bookIssueDate.dd,

        addBookInfoInDataBase.bookIssueDate.mm, addBookInfoInDataBase.bookIssueDate.yyyy);

        found = 1;
          countBook;
    }

fclose(fp);

    if(!found)
    {
        printf("\n\t\t\tNo Record");
    }

    printf("\n\n\t\t\tPress any key to go to main menu.....");

    fflush(stdin);

    getchar();
}

return 0;
}

CodePudding user response:

In the current .c file you showed us, the MAX_BOOK_NAME is not defined. In order to do that you must add the folloing line of code right bellow the #include lines but outside of any function because you want it to be visible int the whole file:

#define MAX_BOOK_NAME the_number_you_want

CodePudding user response:

The problem is that the size of the bookName arrays is nowhere given it is a constant and either needed to be declared after the preprocesser directive like or inside the main function like,

#define MAX_BOOK_NAME = 10; //The number is given just for example

or

const int MAX_BOOK_NAME = 10; //The number is given just for example   

CodePudding user response:

The constant named MAX_BOOK_NAME is not defined in the whole piece of code you shared with us. I guess you should try defining the constant before using it. Try putting :

#define MAX_BOOK_NAME 10// or any number you need
void viewBooks()
{
    int found = 0;

    char bookName[MAX_BOOK_NAME] = {0};

{

or

const int MAX_BOOK_NAME=10//or any number you wish
void viewBooks()
{
    int found = 0;

    char bookName[MAX_BOOK_NAME] = {0};

{

if your wish was to make a dynamic array then you could try this link

if your problem was just not to define the constant then you already got many answers from others as well.

  •  Tags:  
  • c
  • Related