Skip to main content

Command Palette

Search for a command to run...

Functions that return a struct variable

Discount on struct book variables.

Published
•3 min read•View as Markdown
Functions that return a struct variable

In this program we add one more function that takes a struct book variable as one argument, and discount percent as the other argument and returns a struct variable with the discounted price and "on discount!!!" added to book title.

Here is the simple function to achieve it.

struct Book applyDiscount(struct Book book, float discount_percent) {
  struct Book discountedBook = book; // Create a copy of the book struct
  strcat(discountedBook.title, " (on discount!!!)"); // Append discount message

  discountedBook.value *= (1 - discount_percent / 100.0f); // Apply discount

  return discountedBook;
}

We need not create a copy :

struct Book applyDiscount(struct Book book, float discount_percent) {

  strcat(book.title, " (on discount!!!)"); // Append discount message
  book.value *= (1 - discount_percent / 100.0f); // Apply discount

  return book;
}

Complete program:

#include <stdio.h>

#define MAX_TITLE 101
#define MAX_AUTHORS 3
#define MAX_NAME 20

struct Author {
  char first_name[MAX_NAME];
  char middle_initial;
  char last_name[MAX_NAME];
};

struct Book {
  char title[MAX_TITLE];
  struct Author authors[MAX_AUTHORS]; // Nested Author structures
  int num_authors; // Number of authors for this book
  float value;
};

struct Book applyDiscount(struct Book book, float discount_percent);
void readBookDetails(struct Book *book);
void printBookDetails(struct Book book);

int main() {
  struct Book book1;

  readBookDetails(&book1);  // Call the function to read details

  printf("Book details:\n");
  printBookDetails(book1);  // Call the function to print details

  float discount;
  printf("Enter discount percentage (e.g., 10 for 10%% off): ");
  scanf("%f", &discount);

  struct Book discountedBook = applyDiscount(book1, discount);

  printf("Discounted book details:\n");
  printBookDetails(discountedBook);

  return 0;
}

// Function to apply discount and modify title
struct Book applyDiscount(struct Book book, float discount_percent) {
  struct Book discountedBook = book; // Create a copy of the book struct
  strcat(discountedBook.title, " (on discount!!!)"); // Append discount message

  discountedBook.value *= (1 - discount_percent / 100.0f); // Apply discount

  return discountedBook;
}

// Function to read book details from the user
void readBookDetails(struct Book *book) {
  printf("Please enter the book title.\n");
  fgets((*book).title, MAX_TITLE, stdin);
  book->title[strcspn(book->title, "\n")] = '\0'; // Remove trailing newline

  printf("Enter the number of authors (up to %d): ", MAX_AUTHORS);
  scanf("%d", &(*book).num_authors);
  getchar(); // Consume newline character

  // Get information for each author
  for (int i = 0; i < book->num_authors && i < MAX_AUTHORS; i++) {
    printf("Enter details for author %d:\n", i + 1);
    printf("  First name: ");
    fgets(book->authors[i].first_name, MAX_NAME, stdin);
    book->authors[i].first_name[strcspn(book->authors[i].first_name, "\n")] = '\0';

    printf("  Middle initial (or hit space and enter if none): ");
    scanf("%c", &book->authors[i].middle_initial);
    getchar(); // Consume newline character

    printf("  Last name: ");
    fgets(book->authors[i].last_name, MAX_NAME, stdin);
    book->authors[i].last_name[strcspn(book->authors[i].last_name, "\n")] = '\0';
  }

  printf("Please enter the price.\n");
  scanf("%f", &book->value);
}

// Function to print book details
void printBookDetails(struct Book book) {
  printf("%s \nAuthored by : ", book.title);
  // Print author names with commas
  int i;
  for (i = 0; i < book.num_authors; i++) {
    printf(" %s %c %s%s", book.authors[i].first_name,
           book.authors[i].middle_initial, book.authors[i].last_name,
           i < book.num_authors - 1 ? "," : ""); // Comma for all except last
  }
  printf("\n Price Rs. %.2f\n", book.value);
}

Output:


Note:

In the programs in our previous articles, we had #define MAX_TITLE 41 . When we concatenate the (on discount!!!) to it, it overflows into the author.first_name member, and results in the following:

24 views

More from this blog

P

Programming Tutorials

89 posts