Your program will simulate a simple change maker for a vending machine. It will start with a stock of coins and dollars. It will then repeatedly request the price for an item to be purchased or to quit. If given a price, it will accept nickels, dimes, quarters, one-dollar and five-dollar bills—deposited one at a time—in payment. When the user has deposited enough to cover the cost of the item, the program will calculate the coins to be dispensed in change. Alternately, the user can cancel the purchase up until full payment has been deposited, in which case, your program will calculate the coins to be dispensed to refund any partial payment already deposited. With each purchase, the program will update the stock of coins and dollars. Before quitting, it will output the remaining stock of coins and dollars. The specifications are spelled out more thoroughly below. An example interaction with our program appears at the end of this description. All change and refunds must be in coins only, and must use the minimum number of coins possible.
Background
The algorithm for calculating the numbers of coins of each denomination to dispense so as to minimize the total number of coins is an example of a greedy algorithm. Since each coin is at least twice the value of the previous coin, you can start by figuring out the most number of quarters you can dispense (without exceeding the amount), then the most number of dimes, and then the number of nickels. Knowledge of greedy algorithms is not required to complete this assignment. If you are curious, you can read http://en.wikipedia.org/wiki/Greedy_algorithm.
Project Description / Specification
Your program must meet the following specifications:
THIS IS JUST AN EXAMPLE
Sample Interaction (user inputs shown in red):
>>> =========================== RESTART ============================
>>>
Welcome to the vending machine change maker program
Change maker initialized.
Stock contains:
25 nickels
25 dimes
25 quarters
0 ones
0 fives
Enter the purchase price (xx.xx) or `q' to quit: 1.96
Illegal price: Must be a non-negative multiple of 5 cents.
Enter the purchase price (xx.xx) or `q' to quit: 1.95
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 1 dollars and 95 cents
Indicate your deposit: 1
Illegal selection: 1
Payment due: 1 dollars and 95 cents
Indicate your deposit: o
Payment due: 95 cents
Indicate your deposit: o
Please take the change below.
1 nickels
Stock contains:
24 nickels
25 dimes
25 quarters
2 ones
0 fives
Enter the purchase price (xx.xx) or `q' to quit: 3.25
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 3 dollars and 25 cents
Indicate your deposit: o
Payment due: 2 dollars and 25 cents
Indicate your deposit: d
Payment due: 2 dollars and 15 cents
Indicate your deposit: d
Payment due: 2 dollars and 5 cents
Indicate your deposit: o
Payment due: 1 dollars and 5 cents
Indicate your deposit: d
Payment due: 95 cents
Indicate your deposit: c
Please take the change below.
9 quarters
1 nickels
Stock contains:
23 nickels
28 dimes
16 quarters
4 ones
0 fives
Enter the purchase price (xx.xx) or `q' to quit: .05
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 5 cents
Indicate your deposit: f
Please take the change below.
16 quarters
9 dimes
1 nickels
Stock contains:
22 nickels
19 dimes
0 quarters
4 ones
1 fives
Enter the purchase price (xx.xx) or `q' to quit: 25
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 25 dollars and 0 cents
Indicate your deposit: f
Payment due: 20 dollars and 0 cents
Indicate your deposit: f
Payment due: 15 dollars and 0 cents
Indicate your deposit: f
Payment due: 10 dollars and 0 cents
Indicate your deposit: f
Payment due: 5 dollars and 0 cents
Indicate your deposit: c
Please take the change below.
19 dimes
22 nickels
Machine is out of change.
See store manager for remaining refund.
Amount due is: 17 dollars and 0 cents
Stock contains:
0 nickels
0 dimes
0 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: .35
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 35 cents
Indicate your deposit: q
Payment due: 10 cents
Indicate your deposit: d
Please take the change below.
No change due.
Stock contains:
0 nickels
1 dimes
1 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: .35
Menu for deposits:
'n' - deposit a nickel
'd' - deposit a dime
'q' - deposit a quarter
'o' - deposit a one dollar bill
'f' - deposit a five dollar bill
'c' - cancel the purchase
Payment due: 35 cents
Indicate your deposit: q
Payment due: 10 cents
Indicate your deposit: q
Please take the change below.
1 dimes
Machine is out of change.
See store manager for remaining refund.
Amount due is: 5 cents
Stock contains:
0 nickels
0 dimes
3 quarters
4 ones
5 fives
Enter the purchase price (xx.xx) or `q' to quit: q
Total: 29 dollars and 75 cents
>>>
Code:-
#include<iostream>
#include<math.h>
using namespace std;
int nic=25, dim=25, qua=25, one=0, fiv=0; // keeps count of
vending machine coins
int price;
void stockStatus(void); //prints the current status of coins in
vending machine
int inputPrice(void); //takes price input and checks for error or
quit
void printMenu(void); // prints the money input menu
char chooseMenu(void); // takes user input and checks for
error
bool paymentDue(int ); //calculates due payment and takes
input
void giveChange(int ); //gives change according to greedy
algorithm
void total(void); //calculates totals of the vending machine
int main(){
cout<<"Welcome to the vending machine change maker
program"<<endl;
cout<<"Change maker initialized."<<endl;
stockStatus();
while(true){ //infinite loop until user quits
price = inputPrice(); //returns -1 if user presses 'q'
if(price == -1) break; //breaks the loop in case user presses
'q'
printMenu();
if(!paymentDue(price)) //if user cancels the payment mid way,
returns change
giveChange(price);
}
total(); //prints total
return 0;
}
void stockStatus(){
cout<<" Stock contains:"<<endl;
cout<<nic<<" nickels"<<endl;
cout<<dim<<" dimes"<<endl;
cout<<qua<<" quarters"<<endl;
cout<<one<<" ones"<<endl;
cout<<fiv<<" fives"<<endl;
cout<<" "<<endl;
}
int inputPrice(){
cout<<"Enter the purchase price (xx.xx) or 'q' to quit:
";
float f;
cin >> f;
if(cin.fail()) return -1; //returns -1 if 'q' is entered
int x = (int) (f*100.0 + 0.5); //converts decimal to integer and
stores value in cents
if(x % 5 == 0) return x; //checks value divisible by 5
cout << "Illegal price: Must be a non-negative multiple of 5
cents." << endl;
return inputPrice(); //recurses if error
}
void printMenu(){
cout << "Menu for deposits:" << endl;
cout << "'n' - deposit a nickel" << endl;
cout << "'d' - deposit a dime" << endl;
cout << "'q' - deposit a quarter" << endl;
cout << "'o' - deposit a one dollar bill" <<
endl;
cout << "'f' - deposit a five dollar bill" <<
endl;
cout << "'c' - cancel the purchase" << endl;
}
char chooseMenu(){
char ch;
cin >> ch; //takes input
if (ch == 'n' || ch == 'd' || ch == 'q' || ch == 'o' || ch == 'f'
|| ch == 'c') //checks input value
return ch;
cout << "Illegal selection" << endl;
return chooseMenu(); //recurses if wrong input
}
bool paymentDue(int price){
if(price <= 0){ //moves to change menu if no payment is
due
cout<<" Please take the change below." << endl;
giveChange(-price); //calls change method
return true; //payment not cancelled
}
cout << "Payment due: " << price/100 << " dollars
and " << price%100 << " cents" << endl;
char ch = chooseMenu();
switch(ch){
case 'n':
nic++;
price -= 5;
break;
case 'd':
dim++;
price -= 10;
break;
case 'q':
qua++;
price -= 25;
break;
case 'o':
one++;
price -= 100;
break;
case 'f':
fiv++;
price -= 500;
break;
case 'c':
::price -= price; //updates the global price value, to return the
amount already deposited by the user
return false;
}
return paymentDue(price);
}
void giveChange(int price){
if(price == 0){
cout<<"No change due."<<endl;
}
int x;
//order of if statements executes the greedy approach
if(price/500 > 0 && fiv > 0){
x = min(price/500, fiv);
fiv -= x;
price -= 500*x;
cout<<x<<" fives"<<endl;
}
if(price/100 > 0 && one > 0){
x = min(price/100, one);
one -= x;
price -= 100*x;
cout<<x<<" ones"<<endl;
}
if(price/25 > 0 && qua > 0){
x = min(price/25, qua);
qua -= x;
price -= 25*x;
cout<<x<<" quarters"<<endl;
}
if(price/10 > 0 && dim > 0){
x = min(price/10, dim);
dim -= x;
price -= 10*x;
cout<<x<<" dimes"<<endl;
}
if(price/5 > 0 && nic > 0){
x = min(price/5, nic);
nic -= x;
price -= 5*x;
cout<<x<<" nickels"<<endl;
}
if(price > 0){
cout<<"Machine is out of change."<<endl;
cout<<"See store manager for remaining
refund."<<endl;
cout<<"Amount due is: "<<price/100<<" dollars and
"<<price%100<<" cents"<<endl;
}
stockStatus();
}
void total(){
int tot = nic*5 + dim*10 + qua*25 + one*100 + fiv*500;
cout << "Total: " << tot/100 << " dollars and "
<< tot%100 << " cents"<<endl;
}
Code Screenshots:-





Output Screenshot:-


Note: Could you please consider my effort on this work and give up vote. Thank you :)
Your program will simulate a simple change maker for a vending machine. It will start with...