Problem 2: The cost of each item in ArtDesign store is a positive integer number
ID: 3862084 • Letter: P
Question
Problem 2: The cost of each item in ArtDesign store is a positive integer number of dollars (do an input validation using functions). Design a function that accepts the cost of an item and the amount paid and prints how to make change using the smallest possible number of bills of the following denominations: $20, $10, $5, $1. Console Output example: Welcome to ArtDesign store Total: $22 Amount to be Paid: $40 Your change is: $18 $20.00 0 $10.00 1 $5.00 1 $1.00 3 Life is beautiful Enjoy the ride Press any key to continueExplanation / Answer
#include <iostream>
using namespace std;
int getInput() {
int total;
cout<<"Enter the total: $";
cin >> total;
return total;
}
int getAmount() {
int amount;
cout<<"Enter the amount to be paid: $";
cin >> amount;
return amount;
}
int main()
{
cout << "Welcome to the ArtDesign Store...." << endl;
int total = getInput();
if(total <= 0){
cout<<"Invalid total. total must be positive."<<endl;
}
else{
int amount = getAmount();
if(amount <= 0){
cout<<"Invalid amount. Input must be positive."<<endl;
}
else{
int change = amount-total;
cout<<"Your change is $"<<change<<endl;
int change20 = change / 20;
change = change % 20;
int change10 = change / 10;
change = change % 10;
int change5 = change / 5;
change = change % 5;
int change1 = change;
cout<<"$20.00: "<<change20<<endl;
cout<<"$10.00: "<<change10<<endl;
cout<<"$5.00: "<<change5<<endl;
cout<<"$1.00: "<<change1<<endl;
}
}
return 0;
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Welcome to the ArtDesign Store....
Enter the total: $22
Enter the amount to be paid: $40
Your change is $18
$20.00: 0
$10.00: 1
$5.00: 1
$1.00: 3
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.