In: Computer Science
Modify the provided code to create a program that calculates the amount of change given to a customer based on their total. The program prompts the user to enter an item choice, quantity, and payment amount.
Use three functions:
• bool isValidChoice(char) – Takes the user choice as an argument, and returns true if it is a valid selection. Otherwise it returns false.
• float calcTotal(int, float) – Takes the item cost and the quantity as arguments. Calculates the subtotal, and returns the grand total with sales tax (8.25%).
• float calcChange(float, float) – Takes total and the amount paid and returns the change. Amount paid must be higher than the total.
IMPORTANT!
• The functions should not print any value. All the values are printed from the main function.
• Do not modify the main function. Write the functions such that they work in the code provided
Sample Output:
Welcome to the Vend-o-matic! Here are your choices:
a) Ice Cream - $2.75
b) Candy - $1.25
c) Soda - $1.50
d) Gum - $0.50
Please select an item:
b Enter Quantity: -1
ERROR: Invalid Quantity. Please reenter: 2
Total with tax: $2.71
Enter Payment Amount: $2.00
ERROR: Insufficient funds. Please reenter payment: $3.00
Your change due is: $.29
_____________________________________________________________________
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes:
bool isValidChoice(char);
float calcTotal(int, float);
float calcChange(float, float);
int main()
{
char choice;
int quantity;
float total, payment_amount, change, item_cost;
cout << "Welcome to the Vend-o-matic! Here are your
choices:" << endl
<< "a)Ice Cream - $2.75" << endl
<< "b)Candy - $1.25" << endl
<< "c)Soda - $1.50" << endl
<< "d)Gum - $0.50" << endl
<< "Please select an item: ";
cin >> choice;
while(!isValidChoice(choice)) // Function Call
{
cout << "Error: Invalid choice. Please select a valid item:
";
cin >> choice;
}
if(choice == 'a')
{
item_cost = 2.75;
}
else if(choice == 'b')
{
item_cost = 1.25;
}
else if(choice == 'c')
{
item_cost = 1.50;
}
else if(choice == 'd')
{
item_cost = 0.50;
}
cout << endl << "Enter Quantity: ";
cin >> quantity;
while(quantity < 1 || quantity > 20)
{
cout << "ERROR: Invalid Quantity. Please re-enter: ";
cin >> quantity;
}
total = calcTotal(quantity, item_cost); // Function Call
cout << fixed << setprecision(2) << "Total with tax: $" << total << endl;
cout << "Enter Payment Amount: $";
cin >> payment_amount;
while(payment_amount < total)
{
cout << "ERROR: Insufficient funds. Please re-enter payment:
$";
cin >> payment_amount;
}
change = calcChange(total, payment_amount); // Function Call
cout << "Your change is: $" << change;
return 0;
}
// TODO: add function definitions here
#include <iostream>
#include <iomanip>
using namespace std;
// Function Prototypes:
bool isValidChoice(char);
float calcTotal(int, float);
float calcChange(float, float);
int main()
{
char choice;
int quantity;
float total, payment_amount, change, item_cost;
cout << "Welcome to the Vend-o-matic! Here are your choices:" << endl
<< "a)Ice Cream - $2.75" << endl
<< "b)Candy - $1.25" << endl
<< "c)Soda - $1.50" << endl
<< "d)Gum - $0.50" << endl
<< "Please select an item: ";
cin >> choice;
while(!isValidChoice(choice)) // Function Call
{
cout << "Error: Invalid choice. Please select a valid item: ";
cin >> choice;
}
if(choice == 'a')
{
item_cost = 2.75;
}
else if(choice == 'b')
{
item_cost = 1.25;
}
else if(choice == 'c')
{
item_cost = 1.50;
}
else if(choice == 'd')
{
item_cost = 0.50;
}
cout << "Enter Quantity: ";
cin >> quantity;
while(quantity < 1 || quantity > 20)
{
cout << "ERROR: Invalid Quantity. Please re-enter: ";
cin >> quantity;
}
total = calcTotal(quantity, item_cost); // Function Call
cout << fixed << setprecision(2) << "Total with tax: $" << total << endl;
cout << "Enter Payment Amount: $";
cin >> payment_amount;
while(payment_amount < total)
{
cout << "ERROR: Insufficient funds. Please re-enter payment: $";
cin >> payment_amount;
}
change = calcChange(total, payment_amount); // Function Call
cout << "Your change due is: $" << change;
return 0;
}
bool isValidChoice(char ch){
//checks if ch is equal to a,b,c,d then rreturns true
//otherwise returns false
if(ch == 'a' || ch == 'b' || ch == 'c' || ch == 'd'){
return true;
}
else{
return false;
}
}
float calcTotal(int qty, float cost){
//multiply qty with cost and store it in temp variable
float temp = qty * cost;
//calculates 8.25% sales tac
float tax = (qty * cost *8.25)/100;
//calculates final amout with tax and store it in ans variable
float ans = temp + tax;
//return ans
return ans;
}
float calcChange(float total, float payable_amt){
//calculates change amount
float change = payable_amt - total;
//return change
return change;
}
OUTPUT: