In: Computer Science
In this assignment, you need to demonstrate your ability in using input, output, data types, and if statement in C++ program. Assume that you need write a C++ program for a cash register. There are only four items in the store: Cereal, $3.99 Milk, $3.99 Egg, $0.25 Water, $ 1.50 Once a customer purchases items, you will ask her/his how many of them are bought. The quantity can be in the range of 0-10 (including 0 and 10). Then, calculate total for this transaction. Later ask for payment method, which could be either Credit Card or Cash. Do not use string variables. Just use char variables, for instance “1” for Credit Card, “2” for Cash. If the payment method is CC, your program exits. If it is cash, and enter the amount received from customer. Then show the due amount the customer. An example scenario for a CC payment: Enter how many cereal boxes customer bought:2 Enter how many milk jars customer bought:1 Enter how many eggs customer bought:3 Enter how many water bottles customer bought:0 Total is $12.72 Payment Method: 1 Thanks... An example scenario for a cash payment: Enter how many cereal boxes customer bought:1 Enter how many milk jars customer bought:0 Enter how many eggs customer bought:6 Enter how many water bottles customer bought:5 Total is $12.99 Payment Method: 2 Enter the amount received from customer: 20.00 Due amount is $7.01 Thanks...
Program:
#include<iostream>
using namespace std;
int main()
{
//constants that hold price of items
const double PRICE_OF_CEREAL = 3.99;
const double PRICE_OF_MILK = 3.99;
const double PRICE_OF_EGG = 0.25;
const double PRICE_OF_WATER = 1.50;
//variable declarations;
int cerealsBought, milkBought, eggsBought,
waterBought;
double totalPrice, cashReceived,
balanceAmt;
char paymentMethod;
//prompt and read the number of cereal noxes
bought
cout << "Enter how many cereal boxes
customer bought: ";
cin >> cerealsBought;
//prompt and read number of milk jars
bought
cout << "Enter how many milk jars customer
bought: ";
cin >> milkBought;
//prompt and read number of eggs bought
cout << "Enter how many eggs customer
bought: ";
cin >> eggsBought;
//prompt and read number of water bottels
bought
cout << "Enter how many water bottles
customer bought: ";
cin >> waterBought;
//conpute the total price of products
bought
totalPrice = (cerealsBought * PRICE_OF_CEREAL) +
(milkBought * PRICE_OF_MILK) + (eggsBought * PRICE_OF_EGG) +
(waterBought * PRICE_OF_WATER);
//display the total amount
cout << "Total is $" << totalPrice
<< endl;
//prompt and read payment method
cout << "Payment method (1 for Credit
Card, 2 for Cash): ";
cin >> paymentMethod;
//if payment method is 1 terminate the
program
if(paymentMethod == '1')
cout << "Thanks..."
<< endl;
else
{
//read the cash received from
customer
cout << "Enter the amount
received from customer: ";
cin >> cashReceived;
//check if amount entered is
greater than total else reprompt
while(cashReceived <
totalPrice)
{
//display error
message
cout << "Cash
entered is less than total price. Retry.." << endl;
//read the cash
received from customer
cout <<
"Enter the amount received from customer: ";
cin >>
cashReceived;
}
//compute the due amount;
balanceAmt = cashReceived -
totalPrice;
//display the due amount
cout << "Due amount is $"
<< balanceAmt << endl;
cout <<
"Thanks...";
}
return 0;
}
Output:
Program Screenshot: