Can someone help me to create a vending machine program? Im just studying C++ for almost 3 months. We started from cout and finish in looping. It is possible for me to create that kind of program? If not, can you guys suggest me another program? Thanks!
You started from cout and finished in looping? As in, for-loops and such? You can easily learn that in a few days, what did you do for the rest of the class?
Also, this is not a homework site, show us something you have been working on and we'll be glad to help out.
Sorry TarikNeaj. Yes. We do different activities. Also I'm not an IT or CS student. Computer Programming is a minor subject on our course. This is what i'm doing. A vending machine. But I cannot run it. The error says, the variable amount is used without initialization. Hopefully you can help me. Thanks!
Instead of having amount in the while loop why not a boolean like isRunning. Then if you want to stop the while loop just give the user an option that sets isRunning to false, ending the while loop. Or you could check at the end of the while loop if amount == 0 then isRunning = false.
You can use amount if you want, but you need to get the user's amount before the while loop starts, because right now it's not even initialized and if you do initialized it, it would be equal to 0, making it not even run the while loop to start with.
1 2 3 4 5 6 7 8 9
int amount = 0, current = 0;
cin >> amount;
while(amount > 0)
{
//do stuff
}
Thanks sir toast9 it try to use the other. Sorry. Im just a newbie and didn't know about the bool. Our instructor didn't explain that well. So if I use the other method, the codes will look like this?
@Ryanjoshiii. Please always listen to what people have to say and answer their questions, they are asking it for a reason.
I'll just simply explain it.
if (current == 1 || current <= 8)
Is the same as if (current <= 8)
Because numbers. Thats literally it. Number 1 is already less than 8, so if you just used the less than 8 if statement, then the number 1 is already included in it.
current <=8 is all the numbers that is equal to or below 8. 1 is equal to or below 8. Meaning the current == 1 is pointless.
Edit: What exactly are you trying to say with that if statement? Maybe we can help you with that if this is not what you were looking for.
Thank you so much @TarikNeaj. Now I understand. I'm trying to say that the vending machine will display the items if the current amount is exact or more than the amount to pay for each items. And if you still add or insert 1php or 5 php, the machine will continue to display the items that the current amount is capable of buying. I'm so sorry if my codes are not clear.
Yes. If the current amount is 8php, it will display the items that cost 8php, then if the current is 9php, it will display both items that cost 8php up to 9php, and if the current amount is 12php or greater than 12php, it will display items that cost 8php up to 10php. Then you will select one. After that, the machine will display the change.
Also, You can, insert 1php coin or 5php coin until you didn't choose any of other items.
I think this will help you understand what I mean. Please see this.
This is another modification I made. But the problem is, even if the current amount is not enough to buy the items, the items still appear. All I want is the items will appear if the current amount is enough or exact to buy the item. Thanks!
Yes sir. It always appear. I think its because its on the start of the program. I put if else to limit the display items but it doesnt work.
For example sir, I insert 1php coin. So the current amount is 1php. So it means that no items shall appear. But it always appear. So if the current amount is 10php, it will display all the items that worth 8.00php up to 10.00php.