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.
//Twinkie.cpp
//Christina
//assignment 4-4
//calculates change inserted into a vending machine asking for more till sum has been met then gives change
#include <iostream>
using namespace std;
double calc(char amount);
int main()
{
char amount;
double total;
cout<<"For a delicious Deep-Fat Fried Twinkie, Please insert a coin: (q for quarter/d for dime/n for nickel /s for dollar)";
cin>>amount;
do
{
total=calc(amount);
if (total >= 3.5)
cout<<"You have " << total - 3.5 << "in change. \n Enjoy your Twinkie!";
else
{
cout<<"Insert another coin.";
cin>>amount;
}
switch(amount)
{
case 'q':
amount=Q;
break;
case 'd':
amount=D;
break;
case 'n':
amount=N;
break;
case 's':
amount=S;
break;
}
total += amount;
return (total);
}
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(47): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(50): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(53): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(56): warning C4244: '=' : conversion from 'double' to 'char', possible loss of data
1>c:\users\christina\documents\visual studio 2010\projects\c++\chapter 4\christina 4-4\christina 4-4\twinkie.cpp(60): warning C4700: uninitialized local variable 'total' used
No. At least I don't think it would help. The char "amount" is serving a useful purpose. What I suggest in function calc() is define another variable of type double and use that for the calculations instead.
//assignment 4-4
//calculates change inserted into a vending machine asking for more till sum has been met then gives change
#include <iostream>
#include <string>
using namespace std;
double calc(double amount);
int main()
{
double amount,total;
cout<<"For a delicious Deep-Fat Fried Twinkie, Please insert a coin: \n Enter only one of the following (.25/ .10/ .05 / 1) \n";
cin>>amount;
do
{
total=calc(amount);
if (total >= 3.5)
cout<<"You have " << total - 3.5 << "in change. \n Enjoy your Twinkie!";
else
{
cout<<"Insert another coin. \n";
cin>>amount;
}
}while(total < 3.5);
return 0;
}
double calc(double amount)
{
double sum=0;
sum += amount;
return (sum);
}
This is what I changed my code to. If I don't initialize sum I get an error, but the way it runs now it resets the sum everytime so I can't get it to add it each time.