You have invented a vending machine capable of deep frying twinkies. Write a program to simulate the vending machine. It costs $ 3.50 to buy a deep- fried twinkie, and the machine only takes coins in denominations of a dollar, quarter, dime, or nickel. Write code to simulate a person putting money into the vending machine by repeatedly prompting the user for the next coin to be inserted. Output the total entered so far when each coin is inserted. When $ 3.50 or more is added, the program should output Enjoy your deep- fried twinkie along with any change that should be returned. Use top- down design to determine appropriate functions for the program.
#include <iostream>
usingnamespace std;
void twinkie();
bool good(int);
constint TWINKIE_COST = 350;
constint DOLLAR = 100, QUARTER = 25, DIME = 10, NICKEL = 5;
int main()
{
//use a loop to repeat the twinkie machine
char ans;
do
{
//use the machine to get a twinkie.
twinkie();
cout << "Would you like to have another twinkie? (y or n) ";
cin >> ans;
}while (ans == 'y' || ans == 'Y');
return 0;
}
//twinkie vending machine
void twinkie()
{
//declare all variables needed
//initialize where necessary
int total_coins = 0, coin, change;
//a loop to take coins up to 350\
do
{
//prompt user to enter coin
cout << "Enter a coin or dollar amount: ";
cin >> coin;
//validate coin value. make a bool function to validate
if (good(coin))
{
total_coins += coin; //total up coins
cout << "You've added " << total_coins << " cents. " << endl;
cout << "You need " << TWINKIE_COST - total_coins << " more. " << endl;
}
else
{
cout << "Not a valid coin."; //display a message
}
} while (total_coins < TWINKIE_COST);
cout << "Enjoy your twinkie :D ";//display enjoy message
//calculate the change
change = total_coins - TWINKIE_COST;
if (change > 0)
{
cout << "Your change is: " << change;//message
}
return;
}
bool good(int c)
{
if(c==NICKEL || c==DIME || c==QUARTER || c==DOLLAR)
returntrue;
elsereturnfalse;
}