const int TWINKIE_PRICE = 350; This is a function to prompt the user to insert c
ID: 3628827 • Letter: C
Question
const int TWINKIE_PRICE = 350;This is a function to prompt the user to insert coins repeatedly until enough has been paid to buy a twinkie. A switch statement is suggested to use in the function. The total amount inserted, in cents, is returned as integer.
int accept_money();
You may create another to get the change back from total_paid
int compute_change(int total_paid);
The main() function can work like this as suggested:
int main()
{
// Define variable money_entered for the amount of money that the
// user enters, along with change that is to be returned to him.
... ...
// Make sure that monetary values we output are formatted with
// two digits after the decimal point.
... ...
// Call your function accept_money to collect money from the user.
money_entered = accept_money();
// Figure out how much change to return to the user.
change = compute_change(money_entered);
// Dispense the twinkie to the user with change back.
... ...
return 0;
}
==========================================================================
sample output:
===========================================================================
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $0.00.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $1.00.
Next coin: a
'a' is not recognized as a coin.
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $1.00.
Next coin: q
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $1.25.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $2.25.
Next coin: d
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $2.35.
Next coin: D
Please insert coins:
n - Nickel
d - Dime
q - Quarter
D - Dollar
A fried twinkie costs $3.50. You have inserted $3.35.
Next coin: D
Enjoy your deep-fried twinkie. Your change is $0.85
Explanation / Answer
please rate - thanks
#include <iostream>
#include <iomanip>
using namespace std;
int accept_money();
int compute_change(int total_paid);
const int TWINKIE_PRICE = 350;
int main()
{
int money_entered,change;
money_entered=accept_money();
change = compute_change(money_entered);
cout<<" Enjoy your deep-fried twinkie. Your change is $"
<<setprecision(2)<<fixed<<change/100.<<endl;
system("pause");
return 0;
}
int compute_change(int total_paid)
{return total_paid-TWINKIE_PRICE;
}
int accept_money()
{int total=0;
char choice;
do
{
cout<<"Please insert coins: ";
cout<<"n - Nickel ";
cout<<"d - Dime ";
cout<<"q - Quarter ";
cout<<"D - Dollar ";
cout<<"Enter coin: ";
cout<<"A fried twinkie costs $"<<setprecision(2)<<fixed<<TWINKIE_PRICE/100.<<
" You have inserted $"<<setprecision(2)<<fixed<<total/100.<<endl;
cout<<"Next coin: ";
cin>>choice;
switch(choice)
{case 'n': total+= 5; break;
case 'd': total+=10; break;
case 'q': total+=25; break;
case 'D': total+=100; break;
default: cout<<"'"<<choice<<"' is not recognized as a coin. ";
}
}while(total<TWINKIE_PRICE);
return total;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.