Simulate A Vending Machine!

solved.
Last edited on
What are you not getting right?
If you show us the code we might be able to help.
solved.
Last edited on
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
#include <iostream>
#include <iomanip>

using namespace std;

struct drinkInfo { // this struct should be at the top of the file
    string drinkName;
    int totalDrinks;
    float drinkPrice;
};

// Prototype your functions...


// All these three function have the array passed in as a parameter and the size
// of the array.
//
// function1, prints whatever is in the vending machine
void vendingMachine(drinkInfo numDrinks[], const int ARRAY_SIZE);

// function 2, returns a value which is the amount of earnings
float getEarnings(drinkInfo numDrinks[], const int ARRAY_SIZE); 

// function 3, purchases a drink from the machine
void purchaseDrink(drinkInfo numDrinks[], const int ARRAY_SIZE);




void vendingMachine(drinkInfo numDrinks[], const int ARRAY_SIZE) 
{
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        // Here you are just passing through each array and outputting the
        // information on each drink. This should be fairy simple. You must loop
        // the array and print each element out from the array;
    }
}

float getEarnings(drinkInfo numDrinks[], const int ARRAY_SIZE)
{
    // Ok so you are getting the earnings.
    float totalEarnings = 0; // So we declare a float. This is the value we are returning.
    for(int i = 0; i < ARRAY_SIZE; i++)
    {
        // Again, here you want to loop through the array and add up the total
        // amount sold.
        totalEarnings = totalEarnings + numDrinks[i].drinkPrice;
    }
    return totalEarnings;
}

void purchaseDrink(drinkInfo numDrinks[], const int ARRAY_SIZE)
{
    // Remember that we've already displayed the menu of drinks in main, so all
    // the user has to do is select the drink they want to buy.
    int personChoice;
    cout << "Which drink do you want to purchase?"
         << " >> ";
}


int main(){

    int choice;
    const int ARRAY_SIZE = 5;
    // Good job, you've set values for the arrays.
    drinkInfo numDrinks[ARRAY_SIZE] = {{"Coke (can)", 6, 0.75}, {"Coke (bottle)", 6, 1.25}, {"Mountain Dew (can)", 6, 0.75}, {"Water", 6, 1.75}, {"Full Throttle", 6, 2.00}};


// You actually don't need this for loop here... You are developing a menu so I
// would say research on how to build menus. They are actually quite simple.
/*  for(int i=0; i < ARRAY_SIZE; i++){
        vendingMachine(numDrinks, ARRAY_SIZE); // Pass parameters into your function
        cout << "Enter your choice: " << endl;
        cin >> choice;
        if ((choice == 0) || (choice == 1) || (choice == 2) || (choice == 3) || (choice == 4)){
        }
        else {
            break;
        }
    }
*/
    
    /*-----------------------------------------------------------------------------
     *  What you actually need to do here is emulate a menu.
     *-----------------------------------------------------------------------------*/

    bool person_atVendingMachine = true; // Create a boolean and set it to true
    // Since the person is at the vending machine, we want to display drinks, obviously
    // To achieve this, we create a loop
    while(person_atVendingMachine == true) // While the person is at the vending machine
    {
        vendingMachine(numDrinks, ARRAY_SIZE); // We always display the drinks no matter what
        cout << "[1] Purchase drink" << endl <<
                "[2] Show total earnings" << endl <<
                "[3] Leave" << endl;
        cout << "  >> ";
        cin >> choice;
        if(choice == 1)
        {
            // Call your function to purchase a drink
        }
            // Call your function to purchase a drink
        else if(choice == 2)
        {
            // Call your function to show total earnings
        }
        else if(choice == 3)
        {
            // They are leaving the vending machine, so we need to break out of
            // this loop, to do so we just do this
            person_atVendingMachine = false;
        }
        else
        {
            // Display that it was not a choice. Don't insert a break state,
            // though. Simple just display that it wasn't a choice and it should
            // loop over again.
        }
    }
    

    return 0; // You need a return statement here. I've placed one for you.

} // end of main bracket 


You just now have to figure out how you're going to develop the rest of the functions. If you have any questions, feel free to PM me.

Also, please use the correct formatting tools when pasting code.
Last edited on
Topic archived. No new replies allowed.