Professional Documents
Culture Documents
A program to accomplish this is shown in Fig.10.8. The program uses a template to define the
structure of the book. Note that the date of publication, a member of record structure, is also
defined as a structure.
When the title and author of a book are specified, the program searches for the book in the list
using the function
look_up(table, s1, s2, m)
The parameter table which receives the structure variable book is declared as type struct
record. The parameters s1 and s2 receive the string values of title and author while m receives
the total number of books in the list. Total number of books is given by the expression
sizeof(book)/sizeof(struct record)
The search ends when the book is found in the list and the function returns the serial
number of the book. The function returns -1 when the book is not found. Remember that
the serial number of the first book in the list is zero. The program terminates when we
respond "NO" to the question
Do you want any other book?
Note that we use the function
get(string)
to get title, author, etc. from the terminal. This enables us to input strings with spaces such as "C
Language". We cannot use scanf to read this string since it contains two words.
Since we are reading the quantity as a string using the get(string) function, we have to convert it
to an integer before using it in any expressions. This is done using the atoi() function.
Output