bsc

Comprehensive codebase and cou...
Log | Files | Refs | README | LICENSE

pc013.c (2104B)


      1 /*
      2  * Author: Amit Dutta (amitdutta4255@gmail.com) | Date: 29 Mar 2026
      3  * Repo: https://github.com/notamitgamer/bsc
      4  * License: MIT
      5  */
      6 
      7 /* Write a C program that defines a structure named Book with attributes bookID (int),
      8 title (string), and price (float). Include a user-defined function named updatePrice
      9 with the signature: void updatePrice(struct Book *b, float newPrice);
     10  The program should:
     11  * Accept details for one book from the user.
     12  * Display the details before the update.
     13  * Use the function to update the price of the book.
     14  * Display the updated details.*/
     15 
     16 #include <stdio.h>
     17 #include <stdlib.h>
     18 #include <string.h>
     19 
     20 typedef struct Book
     21 {
     22     int bookID;
     23     float price;
     24     char title[20];
     25 } Book;
     26 
     27 void acceptDetails(Book *);
     28 void displayDetails(Book *);
     29 void updatePrice(struct Book *, float);
     30 
     31 int main()
     32 {
     33     Book book1;
     34     float newPrice;
     35     acceptDetails(&book1);
     36     displayDetails(&book1);
     37     printf("\n\nEnter the updated price: ");
     38     if (scanf("%f", &newPrice) != 1)
     39     {
     40         printf("\nInvalid Price!");
     41         exit(1);
     42     }
     43     updatePrice(&book1, newPrice);
     44     displayDetails(&book1);
     45     return 0;
     46 }
     47 
     48 void acceptDetails(Book *book)
     49 {
     50     char *p;
     51     printf("\n== Enter details for the book ==\n");
     52     printf("Enter Book ID:    ");
     53     if (scanf("%d", &book->bookID) != 1)
     54     {
     55         printf("\nInvalid Book ID!");
     56         exit(1);
     57     }
     58     while (getchar() != '\n')
     59         ;
     60     printf("Enter Book Title: ");
     61     if (fgets(book->title, sizeof(book->title), stdin) == NULL)
     62     {
     63         printf("\nError reading input!");
     64         exit(1);
     65     }
     66     p = strchr(book->title, '\n');
     67     if (p)
     68         *p = '\0';
     69     printf("Enter Price:      ");
     70     if (scanf("%f", &book->price) != 1)
     71     {
     72         printf("\nInvalid Price!");
     73         exit(1);
     74     }
     75 }
     76 
     77 void displayDetails(Book *book)
     78 {
     79     printf("\n== Details of the Book ==");
     80     printf("\nBook ID:    %d", book->bookID);
     81     printf("\nBook Title: %s", book->title);
     82     printf("\nBook Price: %g", book->price);
     83 }
     84 
     85 void updatePrice(struct Book *book, float newPrice)
     86 {
     87     book->price = newPrice;
     88 }
© notamitgamer • Site Built: 2026-07-21 13:58:23 UTC • git-mirror commit: 1037f62 [view raw info]
Originally created with stagit • modified by notamitgamer
Forked from github.com/notamitgamer/git-mirror