Question

Write a C++ program that asks for the following information about a book order from the...


Write a C++ program that asks for the following information about a book order from the console:
• Title
• Author
• ISBN (hint: careful about what data type to use - validate 9 or 13 characters)
• Price (validate number < 400)
• Quantity (number of books to purchase – validate > 0 and < 100)
• Fiction/Non-Fiction (‘N’ or ‘F’ - validate)
• Genre (‘R’ romance, ‘D’ drama, ‘M’ mystery – validate)
Make use of the following data types:
• C++ string
• double
• int
• char
• bool
Additional Requirements:
• Assume the tax on the total price of the books is 7%
• If the quantity is 90-100: Fee $50, 80-89 Fee $40, 70-79: Fee $30, 60-69: Fee $20, less than 60 Fee $10
• Use the integer division and switch statement to determine the fee.
• Tax should not be calculated on the fees.
• A Boolean variable should be used to hold whether the book is Fiction or Non-fiction
• If the user puts in non-valid data for any of the questions, exit the program. (We will use loops in the next unit to re-ask the question)
• Prompt the user for Fiction/Non-Fiction and Genre in a clear manner explaining the char characters to enter for their choice (give a menu).
• Use an if/else if statement to display the genre in the output
• If the book is a non-fiction romance, remove the fee (this if statement should be separate and after the switch statement for the fee.
Example Output
(I will use different inputs when I test your program, however you can use this data to test your work.) Invoice (include the lines) to console:
_________________________
Invoice
Introduction to C++ Tony Gaddis
ISBN: 000345664 Non-Fiction Mystery
Print
Price:
Quantity:
Subtotal:
Tax:
Fees:
Total: ___________________________
170.50
0 0
Add a comment Improve this question Transcribed image text
Answer #1

// C++ program to create an application for ordering a book
#include <iostream>
#include <cctype>
#include <iomanip>
using namespace std;

int main() {

   string title, author, isbn;
   double price, tax;
   int quantity;
   bool fiction;
   char fictionInput;
   char genre;
   double fee;
   double subtotal;

   // input of book details
   cout<<"Enter book details : "<<endl;
   // input of title
   cout<<"Title : ";
   getline(cin,title);
   title = title.substr(0,title.length()-1); // remove '\n' from the end

   // input of author
   cout<<"Author : ";
   getline(cin,author);
   author = author.substr(0,author.length()-1); // remove '\n' from the end

   // input of isbn
   cout<<"ISBN : ";
   cin>>isbn;
   // validate the number of characters in isbn is 9 or 13
   if(isbn.length() != 9 && isbn.length() != 13)
   {
       cout<<"Invalid ISBN"<<endl;
       return 1;
   }

   // input of price
   cout<<"Price : ";
   cin>>price;

   // validate price < 400
   if(price >=400)
   {
       cout<<"Invalid price"<<endl;
       return 1;
   }

   // input of quantity
   cout<<"Quantity : ";
   cin>>quantity;
   // validate quantity >0 and quantity < 100
   if(quantity < 0 || quantity >= 100)
   {
       cout<<"Invalid quantity"<<endl;
       return 1;
   }
   // input of fiction
   cout<<"Fiction(F) / Non-fiction(N) :";
   cin>>fictionInput;

   // validate fiction input
   if((toupper(fictionInput) != 'F') && (toupper(fictionInput) != 'N'))
   {
       cout<<"Invalid Fiction input"<<endl;
       return 1;
   }

   // validate fiction input
   if(fictionInput == 'F')
       fiction = true;
   else
       fiction = false;

   // input of genre
   cout<<"Genre ('R'- romance, 'D' - drama, 'M' - mystery) : ";
   cin>>genre;

   // validate the genre
   if((toupper(genre) != 'R') && (toupper(genre) != 'D') && (toupper(genre) != 'M'))
   {
       cout<<"Invalid Genre"<<endl;
       return 1;
   }

   // calculate subtotal
   subtotal = quantity*price;
   // calculate tax
   tax = subtotal*0.07;

   // determine the fee
   int feeIndicator = quantity/10;
   switch(feeIndicator)
   {
       case 9:
           fee = 50;
           break;
       case 8:
           fee = 40;
           break;
       case 7:
           fee = 30;
           break;
       case 6:
           fee = 20;
           break;
       default:
           fee = 10;
   }

   // if book is non-fiction and genre is romance then remove the fee
   if((!fiction) && (toupper(genre) == 'R'))
       fee = 0;

   // output the details
   cout<<fixed<<setprecision(2);
   cout<<"Invoice : "<<endl;
   cout<<title<<" "<<author<<endl;
   cout<<"ISBN : "<<isbn;
   if(fiction)
       cout<<" Fiction ";
   else
       cout<<" Non-Fiction ";

   if(toupper(genre) == 'R')
       cout<<"Romance";
   else if(toupper(genre) == 'D')
       cout<<"Drama";
   else
       cout<<"Mystery";
   cout<<endl;
   cout<<"Price : "<<price<<endl;
   cout<<"Quantity : "<<quantity<<endl;
   cout<<"SubTotal : "<<subtotal<<endl;
   cout<<"Tax : "<<tax<<endl;
   cout<<"Fees : "<<fee<<endl;
   cout<<"Total : "<<(subtotal+tax+fee)<<endl;

   return 0;
}
//end of program

Output:

Add a comment
Know the answer?
Add Answer to:
Write a C++ program that asks for the following information about a book order from the...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • please use C++ write a program to read a textfile containing a list of books. each...

    please use C++ write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. each line in the file has tile, ..... write a program to read a textfile containing a list of books. each line in the file has tile, ... Question: Write a program to read a textfile containing a list of books. Each line in...

  • MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access...

    MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...

  • MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access...

    MUST BE WRITTEN IN C++ Objective: Learn how to define structures and classes, create and access a vector of structures, use sort with different compare functions. Assignment: Your program will use the same input file, but you will print the whole book information, not just the title. And, most importantly, this time you will take an object oriented approach to this problem. Detailed specifications: Define a class named Collection, in which you will define a structure that can hold book...

  • Write a C++ program that will calculate the total amount of money for book sales at...

    Write a C++ program that will calculate the total amount of money for book sales at an online store. For each sale, your program should ask the number of books sold and then the price for each book and the shipping method (‘S’ for standard shipping and ‘E’ for expedited shipping). The program will produce a bill showing the subtotal, the sales tax, the discount, the shipping charge, and the final total for the sale. The program will continue processing...

  • Need a program in python. Asterix and Obelix want to store and process information about movies...

    Need a program in python. Asterix and Obelix want to store and process information about movies they are interested in. So, you have been commissioned to write a program in Python to automate this. Since they are not very computer literate, your program must be easy for them to use. They want to store the information in a comma separated text file. Using this they would need to generate a report – see all items AND show a bar chart...

  • Modify your Online Book Order program from week 4 to prompt the users for the number...

    Modify your Online Book Order program from week 4 to prompt the users for the number of books they are purchasing. Using a for loop, prompt the user for the cost of each book and store it into a subtotal. Use the subtotal and the number of books to pass to your method. No changes should be needed to your method or parameters. Output a receipt formatted as the following: Enter the number of books purchased: 4 Enter the book...

  • 8. (15 marks) Write a complete C program, which uses an array of structures and two...

    8. (15 marks) Write a complete C program, which uses an array of structures and two user defined functions. The program deals with a library database. The program will ask the user to input required information about each book and store it in a structure in a user defined function called get info. The second user defined function display_info will display database information on the screen. The output should look as follows: Book Title Book ld 123456 C Programming 10...

  • Write a C++ program that uses: .selection constructs (if, if else, and switch) .multiway branches .looping...

    Write a C++ program that uses: .selection constructs (if, if else, and switch) .multiway branches .looping constructs (do and while loops) .Boolean expressions, bool and char types. 1. Develop a program that calculates charges for an Internet provider. The cost is determined by the amount of data used and the consumer plan. Package B (basic) is the most affordable if you plan on using < 20GB (20,000MB), however, if you consume more than 20GB (20,000MB) you are charged for each...

  • write a c++ program. A real estate broker sets fees for selling properties according to the...

    write a c++ program. A real estate broker sets fees for selling properties according to the property type as shown in the table. The broker wants you to write a program that prompts the user for the broker type, sale price of the property and the property code. If the user enters an invalid property code or broker code, the program should display an appropriate message and end the program. Allow the user to enter uppercase and lowercase letters. If...

  • . Use what you learned from our First Python Program to write a Python program that...

    . Use what you learned from our First Python Program to write a Python program that will carry out the following tasks. Ask user provide the following information: unit price (for example: 2.5) quantity (for example: 4) Use the following formula to calculate the revenue: revenue = unit price x quantity Print the result in the following format on the console: The revenue is $___. (for example: The revenue is $10.0.) . . Use the following formula to calculate the...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT