#include #include #include #include //whenever we use variable type 'string,' we need to #include it. using namespace std; struct Book { // The blueprint for a structure containing multiple fields. string title; // String variable type since the values will be words. string author; // refer to above about string. double price; // Price in US dollars, we need double to show the decimal // places. bool bestseller; // I use bool here to indicate that this is either true or // false. }; void applyDiscount(Book *p, double discountPercentage); //declaring the function before the main function, so we can let the program know to expect it. void print (const Book *p); //declaring the print function for use later on. int main() { Book novel {"Holmes Is Missing", "James Patterson", 30.00, true}; //calling the structure and giving values to the variables. print(&novel); //calling the print function from above. string userInput; // we use string because the input will be a word and not a numerical value. cout << "Would you like to use your discount code? (yes/no):\n\n"; cin >> userInput; // This is where the user inputs the code. if (userInput == "yes") { //If the user's answer is equivalent to the word 'yes' applyDiscount(&novel, 30.0); // Apply a 30% discount cout << "Discount applied!\n"; //program will output this after applying the discount. } else if (userInput == "no") { //if the user, for some reason doesn't want to save money... cout << "No discount applied.\n"; // the program output this statement. } else { cout << "Invalid input. No discount applied.\n"; //The user will get hit with an output, not an error //message (which would stop the program after EXIT_FAILURE } cout << "After checking discount option:\n\n"; print(&novel); return EXIT_SUCCESS; } void applyDiscount(Book *p, double discountPercentage) { // Apply the discount to the price p->price *= ( 100 - discountPercentage )/ 100; // Append to the title if it's a bestseller if (p->bestseller) { p->title += " (Bestseller)"; } } void print(const Book *p) //This function cannot change the structure. { cout << "Title: " << p->title << "\n" << "Author: " << p->author << "\n" << "Price: $" << fixed << setprecision(2) << p->price << "\n" << "Bestseller: " << (p->bestseller ? "Yes" : "No") << "\n\n"; }