Vending Machine

So I'm close to finishing a vending machine program Iv'e been working on for a while now. I'm pretty new to C++ and need help with a few things on the program. The first thing being, having an admin screen with a secret password that tracks stock, lets you change and add more stock, and tracks money. So how would I go about having a password to enter the admin screen and how do I keep track of the money spent on the vending machine? Here is my code if you see anything wrong with it let me know too:

//
// Vending Machine
// Description: A vending machine program that lets the user buy a product, tells them how much the product is, keeps track of stock, has an admin screen where the owner can restock the machine, and tells the user if they have enough money.

// Created On: Oct. 24th
// Last Edit: Nov. 3rd
// Created by: Mack Horgan
// Known Limitations:

#include <iostream>
using namespace std;

int main() {




int product1 = 79.99;
int product2 = 59.99;
int product3 = 129.00;
int product4 = 49.99;
int product5 = 12.99;
int product6 = 4.99;
int product7 = 55.00;
int product8 = 30.99;
int product9 = 19.99;
int stock1 = 5;
int stock2 = 5;
int stock3 = 5;
int stock4 = 5;
int stock5 = 5;
int stock6 = 5;
int stock7 = 5;
int stock8 = 5;
int stock9 = 5;
char vendmachinepass;
int customermoney = 0.00

// Vending machine graphic

if (vendmachinepass) {

cout << "Welcome to the admin screen. Here you can edit and keep track of stock. ";
}

cout << "How much money do you have? ";
cin >> customermoney;

while (customermoney >= 4.99 && customermoney == 129.00) { // While Loop

int customerchoice;

cout << "Which game would you like to buy? Enter '1' for Battlefield 1, enter '2' for No Man's Sky, enter '3' for Battlefield 1: Premium Edition, enter '4' for Grand Theft Auto V, enter '5' for Undertale, enter '6' for Fallout: New Vegas, enter '7' for The Withcher 3, enter '8' for Tomb Raider, or enter '9' for Rocket League. ";
cin >> customerchoice; // User enters number for corresponding game

if (customerchoice == '1') {

cout << "You have selected Battlefield 1. ";
customermoney = customermoney - product1; // Calculation for how much the user has after purchase (1 - 9)
cout << "You have $ " << customermoney << "left. "; // Tells user their change after purchase (1 - 9)

stock1 = stock1 - 1; // Calculation for keeping track of stock (1 - 9)


}

else if (customerchoice == '2') {

cout << "You have selected No Man's Sky. ";
customermoney = customermoney - product2;
cout << "You have $ " << customermoney << "left. ";

stock2 = stock2 - 1;

}

else if (customerchoice == '3') {

cout << "You have selected Battlefield 1: Premeium Edition. ";
customermoney = customermoney - product3;
cout << "You have $ " << customermoney << "left. ";

stock3 = stock3 - 1;
}

else if (customerchoice == '4') {

cout << "You have selected Grand Theft Auto V. ";
customermoney = customermoney - product4;
cout << "You have $ " << customermoney << "left. ";

stock4 = stock4 - 1;
}

else if (customerchoice == '5') {

cout << "You have selected Undertale. ";
customermoney = customermoney - product5;
cout << "You have $ " << customermoney << "left. ";

stock5 = stock5 - 1;

}

else if (customerchoice == '6') {

cout << "You have selected Fallout: New Vegas. ";
customermoney = customermoney - product6;
cout << "You have $ " << customermoney << "left. ";

stock6 = stock6 - 1;
}

else if (customerchoice == '7') {

cout << "You have selected The Witcher 3. ";
customermoney = customermoney - product7;
cout << "You have $ " << customermoney << "left. ";

stock7 = stock7 - 1;

}


else if (customerchoice == 8) {

cout << "You have selected Tomb Raider. ";
customermoney = customermoney - product8;
cout << "You have $ " << customermoney << "left. ";

stock8 = stock8 - 1;

}

else if (customerchoice == '9') {

cout << "You have selected Rocket League. ";
customermoney = customermoney - product9;
cout << "You have $ " << customermoney << "left. ";

stock9 = stock9 - 1;

}

else if (customermoney <= 4.98 && customermoney == 0.00) {

cout << "Sorry, you don't have enough money to purchase anything ";
}



} // End of while loop



return 0;
}

Last edited on
You should use an array and avoid repeated code.
while (customermoney >= 4.99 && customermoney == 129.00) { // While Loop


It looks like this loop will only execute if customermoney is greater than 4.99 and equal to 129.00. Will the user only ever have 129.00? This loop will only execute if the input is 129.00 every time.

Also, it is in a while loop that has no obvious escape and I bet if you get 35.00 as an input for customermoney it will not execute the loop. Try removing the 2nd condition, that should clean up the 129.00 issue and allow the loop to execute until the money drops below the amount set by the condition.

You kinda have the same problem on your last else if statement. If your lowest price item is 4.99, you don't need the 0.00 portion since 0.00 will always be lower than 4.98. This statement will only execute if the remaining customer money is == to 0.00.
Topic archived. No new replies allowed.