PLEASE USE 2 FUNCTIONS !!!!! and bool.. C++ Help You have invented a vending mac
ID: 3679245 • Letter: P
Question
PLEASE USE 2 FUNCTIONS !!!!! and bool.. C++ Help
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. Allow the buyer to purchase again if desired.
IMPLEMENTATION:
Use at least two functions: one to accept money and one to make change.
Validate the denominations.
WELCOME TO TWINKIE VENDING MACHINE
TWINKIES are $3.50 each.
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $0.00.
Next coin: n
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $0.05.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $1.05.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $2.05.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $3.05.
Next coin: q
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried TWINKIE costs $3.50. You have inserted $3.30.
Next coin: q
You have inserted $3.55
Enjoy your deep-fried TWINKIE. Your change is $0.05
Would you like to purchase another?
Explanation / Answer
Hi,
The following code will help you to run the requested solution. The same code was tested in online at http://cpp.sh/.
#include <iostream>
using namespace std;
void twinkie();
bool good(float);
char nextcoin(float cur);
const float COST = 350;
const float DOLLAR = 100, QUARTER = 25, DIME = 10, NICKLE = 5;
int main()
{
char answer;
do{
twinkie();
cout << " Would you like to purchase another? (y or n) ";
cin >> answer;
}while (answer == 'y' || answer == 'Y');
return 0;
}
void twinkie()
{
float total_coin = 0, coin, change;
char coid;
cout << "WELCOME TO TWINKIE VENDING MACHINE ";
cout << "TWINKIES are $3.50 each. ";
do
{
cout << " Please insert coins: ";
cout << "n - Nickel ";
cout << "d - Dime ";
cout << "q - Quarter ";
cout << "D - Dollar ";
cin >> coid;
switch(coid)
{
case 'n':
{
coin=5;
break;
}
case 'd':
{
coin=10;
break;
}
case 'q':
{
coin=25;
break;
}
case 'D':
{
coin=100;
break;
}
default:
{
}
}
if (good(coin))
{
total_coin += coin;
cout <<"A fried TWINKIE costs $3.50.";
cout << " You have inserted $" << (total_coin/100) << "." << endl;
if((COST - total_coin)>0)
{
cout << " Next Coin:" << nextcoin(COST - total_coin) << endl;
}
}
else
{
cout << "Not a valid coin.";
}
}
while (total_coin < COST);
cout << " Enjoy your deep-fried TWINKIE. ";
change = total_coin - COST;
if (change > 0)
{
cout << " Your change is: $" << (change/100);
}
return;
}
bool good(float cur)
{
if (cur == NICKLE || cur == DIME || cur == QUARTER || cur == DOLLAR)
return true;
else
return false;
}
char nextcoin(float cur)
{
if(cur>=100)
{
return 'D';
}
else
{
if(cur>=25)
{
return 'q';
}
else
{
if(cur>=10)
{
return 'd';
}
else
{
return 'n';
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.