How to do this structures, any Idea?
Given a program segment as follows:

#include <stdio.h>

struct book {
char title[80];
int bar_code;
float price;
} ;

struct book BOOK;

void INPUT (struct book *B);
float DISCOUNT (struct book *B);


You are required to write a complete program that includes the following functions:

i. INPUT() function to read the book information.
ii. DISCOUNT() function to calculate discount price for a book. If the price is more than RM100, 10% discount will be given.
iii. main() function to call INPUT and DISCOUNT function. Then, display all information stored in the BOOK structure and the discount price.

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

struct BOOK
{
    char catalog_number[20];
    char title[80];
    char author [40];
    float price;
    int year_published;
} book[100];

void input_books_info(int number_of_books)
{
    for (int i = 0; i < number_of_books; i++)
    {
        printf("%d book catalog number: ", i + 1);
        scanf("%s", &book[i].catalog_number);
        printf("%d book title: ", i + 1);
        scanf("%s", &book[i].title);
        printf("%d book author: ", i + 1);
        scanf("%s", &book[i].author);
        printf("%d book price: ", i + 1);
        scanf("%f", &book[i].price);
        printf("%d book year published: ", i + 1);
        scanf("%d", &book[i].year_published);
    }
}

void print_books_info(int number_of_books)
{
    printf("n");
    for (int i = 0; i < number_of_books; i++)
    {
        printf("%s, ", book[i].catalog_number);
        printf("%s, ", book[i].title);
        printf("%s, ", book[i].author);
        printf("%f, ", book[i].price);
        printf("%dn", book[i].year_published);
    }
}

void search_for_book(char book_catalog_number[20], int number_of_books)
{
    printf("n");
    for (int i = 0; i < number_of_books; i++)
    {
        if (strcmp(book[i].catalog_number, book_catalog_number) == 0)
        {
            printf("%s, ", book[i].catalog_number);
            printf("%s, ", book[i].title);
            printf("%s, ", book[i].author);
            printf("%f, ", book[i].price);
            printf("%dn", book[i].year_published);
        }
    }
}

int main ( )
{
    int number_of_books;
    do
    {
        printf("input number of books: ");
        scanf("%d", &number_of_books);
    } while (number_of_books < 1 || number_of_books > 100);
    
    input_books_info(number_of_books);
    print_books_info(number_of_books);
    
    char book_catalog_number[20];
    printf("n");
    printf("search book with catalot nomber: ");
    scanf("%s", book_catalog_number);
    search_for_book(book_catalog_number, number_of_books);
}

hi westony..thanks for the help. I have run this coding in turbo c but still have an error in line 15, 33, 46, 71 and 74. how to solve the error?

after making a structure we can use functions also we use pointer for memory allocation.and reduce coding.

already done but still error on 71 expression syntax in function main,
74 undefine symbol"book_...."in function.

Take all chars and ints at the start of the functions ... see that this char is in the end move it at the start of the function!

Now i can run the prog but how about the DISCOUNT() function to calculate discount price for a book. If the price is more than RM100, 10% discount will be given...

Hi Westony,
I have problem with line 43
still error on 43 expression syntax in function print_book_info..

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

struct BOOK
{
    char catalog_number[20];
    char title[80];
    char author [40];
    float price;
    int year_published;
} book[100];
  char book_catalog_number[20];

void input_books_info(int number_of_books)
{
    int i;
    for (i = 0; i < number_of_books; i++)
    {
        printf("%d book catalog number: ", i + 1);
        scanf("%s", &book[i].catalog_number);
        printf("%d book title: ", i + 1);
        scanf("%s", &book[i].title);
        printf("%d book author: ", i + 1);
        scanf("%s", &book[i].author);
        printf("%d book price: ", i + 1);
        scanf("%f", &book[i].price);
        printf("%d book year published: ", i + 1);
        scanf("%d", &book[i].year_published);
    }
}

void print_books_info(int number_of_books)
{
    int i;
    printf("n");
    for (i = 0; i < number_of_books; i++)
    {
        printf("%s, ", book[i].catalog_number);
        printf("%s, ", book[i].title);
        printf("%s, ", book[i].author);
        printf("%f, ", book[i].price);
        printf("%dn", book[i].year_published);
    }
}

void search_for_book(char book_catalog_number[20], int number_of_books)
{
    int i;
    printf("n");
    for (i = 0; i < number_of_books; i++)
    {
        if (strcmp(book[i].catalog_number, book_catalog_number) == 0)
        {
            printf("%s, ", book[i].catalog_number);
            printf("%s, ", book[i].title);
            printf("%s, ", book[i].author);
            printf("%f, ", book[i].price);
            printf("%dn", book[i].year_published);
        }
    }
}

int main ( )
{
    int number_of_books;
    do
    {
        printf("input number of books: ");
        scanf("%d", &number_of_books);
    } while (number_of_books < 1 || number_of_books > 100);

    input_books_info(number_of_books);
    print_books_info(number_of_books);


    printf("n");
    printf("search book with catalog number: ");
    scanf("%s", book_catalog_number);
    search_for_book(book_catalog_number, number_of_books);

}

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Hi westony,
How about the discount function (), let say if more than RM200 10% discount will be given

This question has already been answered. Start a new discussion instead.