Write a Program: Invoice
A customer makes a purchase at the store. The tax rate is 8% on all purchases
except groceries. The store offers a 5% discount to seniors 60 and over. You
should ask the user three questions: what they have bought (use a menu
displaying bread, milk, or soap), what the purchase price is, and how old they are.
Your goal is to print a formatted invoice with the following line items: price, tax,
discount, and total. The discount should only be printed if applicable.
You should test your program with 5 data sets: with and without tax and with and
without the senior discount. The fifth data set makes an invalid choice of the
menu item, in which case your program should not ask any more questions and
output an error message.
#include <iostream>
#include <string>
using namespace std;
int main(){
const double TAX_RATE = 0.08; //The tax rate is 8% on all purchases except groceries
const double SENIOR_DISCOUNT = 0.05; //The store offers a 5% discount to seniors 60 and over.
int choice;
double price;
double tax;
double discount;
double total;
int age;
cout<<"What would you like to buy?\n";
cout<<"1. Bread\n";
cout<<"2. Milk\n";
cout<<"3. Soap\n";
cout<<"Please enter your choice: ";
cin>>choice;
if(choice>=1 && choice<=3){
cout<<"Please enter the price $";
cin>>price;
cout<<"Please enter your age: ";
cin>>age;
tax=0;
if(choice>=1 && choice<=2){
tax=TAX_RATE*price;
}
cout<<"\n\t\tInvoice\n";
cout<<"Price: $"<<price<<"\n";
cout<<"Tax: $"<<tax<<"\n";
discount=0;
if(age>=60){
discount=SENIOR_DISCOUNT*price;
cout<<"Discount: $"<<discount<<"\n";
}
total=price+tax-discount;
cout<<"Total: $"<<total<<"\n";
}else{
cout<<"\nInvalid choice of the menu item.\n\n";
}
system("pause");
return 0;
}
Comments
Leave a comment