### 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
 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126`` ``````#include #include 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