What's wrong with this??

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
using namespace std;

void twinkie();
bool good(int);

const int TWINKIE_COST = 350;
const int 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)
             return true;
     else
             return false;
}
Line 32. The '\' at the end makes the preprocessor append line 33 to line 32. Therefore, 'do' is commented out.
Last edited on
WOW Thanks!
Topic archived. No new replies allowed.