Question: The question is: Used c++ to solve, write a program that computes how much a customer has to pay after purchasing two items. The price
The question is:
Used c++ to solve, write a program that computes how much a customer has to pay after purchasing two items.
The price is calculated according to the following rules: Buy one get one half off promotion: the lower price item is half price. If the customer is a club card member, additional 10% off. Tax is added. Inputs to the program include: Two items prices Have club card or not (User enters Y or y for yes; N or n for no) Tax rate (User enters the percentage as a number; for example, they enter 8.25 if the tax rate is 8.25%)
Program displays: Base price - the price before the discounts and taxes Price after discounts - the price after the buy one get one half off promotion and the members discount, if applicable Total price the amount of money the customer has to pay (after tax) printed with the precision of 2 decimal digits.
Hint: In order to print a number in a specific precision, you can use the round function passing 2 arguments to it. To format to two decimal places you can use the printf function with the format of 2.2f.
For example, an execution could look like this: Enter price of the first item: 10 Enter price of the second item: 20 Does customer have a club card? (Y/N): y Enter tax rate, e.g. 5.5 for 5.5% tax: 8.25 Base price = 30.00 Price after discounts = 22.50 Total price = 24.36
Score
There are five tests each worth 2 points
And my answer is:
#include
using namespace std;
int main() { // INPUT THE PRICES FOR BOTH THE ITEMS double i1, i2; cout << "Enter price of the first item: "; cin >> i1; cout << "Enter price of the second item: "; cin >> i2;
// to get the base price double base_price = i1 + i2;
// club card check char club_card; cout << "Does customer have a club card? (Y/N): "; cin >> club_card;
// price after discounts double price_after_discounts; if (i1 < i2) { // If the first item is cheaper, apply the buy one get one half off promotion price_after_discounts = i1 + (i2 / 2); } else { // If the second item is cheaper, apply the buy one get one half off promotion price_after_discounts = (i1 / 2) + i2; } if (club_card == 'Y' || club_card == 'y') { // If the customer has a club card, apply an additional 10% discount price_after_discounts *= 0.9; }
// Get the tax rate double tax_rate; cout << "Enter tax rate, e.g. 5.5 for 5.5% tax: "; cin >> tax_rate; // Calculate the tax amount double tax = price_after_discounts * (tax_rate / 100); // Calculate the total price double total_price = price_after_discounts + tax;
// Round the prices to two decimal places base_price = round(base_price * 100) / 100; price_after_discounts = round(price_after_discounts * 100) / 100; total_price = round(total_price * 100) / 100;
// Print the results printf("Base price = %.2f ", base_price); printf("Price after discounts = %.2f ", price_after_discounts); printf("Total price = %.2f ", total_price);
return 0; }
But the system showed that:
Test Failed: 'base price = 30.00 price after discounts = 18.00 total price = 19.49' != 'base price = 30.00 price after discounts = 22.50 total price = 24.36' - base price = 30.00 price after discounts = 18.00 total price = 19.49 ? ^^ - ^^ ^^ + base price = 30.00 price after discounts = 22.50 total price = 24.36 ? ^^ + ^^ ^^
I wonder how to improve my answers, thank you!
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
