Vending Machine Struct Array Functions

Hi guys, I'm trying to get this program to run properly but it just doesn't want to accept the user input and quit the loop, or it accepts the input after several times of the user entering it. I need it to accept input from the user to select a drink, remove 1 from the remaining drink count, give the user back their change, and print out how much the machine made after the user selects quit. I'm thinking it has something to do with the do-while loop and there should be a while loop in main. Any help will be appreciated.
Last edited on
The reason you have to input a number twice at the beginning is because you are calling showMenu (selectionChoice); in main (which isn't being assigned to anything, so it goes to the function then exits). Then you call it again in your transactionProcess function which works correctly. Delete the call in main.

Also this loop isn't really enough to handle all of the inputs
while (moneyPaid <= .00 || moneyPaid > 1.00)
This lets the user enter in less than the price of the drink and still works, you will want to put another limit there.

You don't really need a separate if statement for every drink that is entered, you could do something faster like this, but up to you.
if(choice >= 1 && choice <= 5)
	if (array[choice-1].drinksLeft <= 0)
		cout << endl;
		cout << "Product sold out, please enter another selection.\n";

		array[choice-1].drinksLeft = array[choice-1].drinksLeft - 1;

Thank you, that helped a lot.
I removed the call in main, changed the moneyPaid loop, and used your array[choice -1] simplification.

After running it I still have to select 6 multiple times for it to exit and display the thank you message. What could be causing that? This only happens after I run through the process a third time.
Last edited on
It seems to work correctly for me when I enter 6, you might want to try posting your code again so I can double check?
It happens when I run through it a few times and more frequently when I select different drink options.
Last edited on
Topic archived. No new replies allowed.