Write a Function That Will Help a Vending Machine
How to make vending machine program
Hello. I was assigned a project to make a program that will be like a vending machine. The vending machine has 3 drinks. With only 10 bottles of each. It has to keep on asking what flavor you want till the drink runs out. If just one drink runs out, it has to tell you to make another selection. If all run out, it must end the program. It also has to ask to put in change with dollar bills, quarters, nickels, and dimes. It has to give you change too. I have started making it, but have no idea what I am doing. I can't get it to stop working after you select a drink 10 times. I have tried for loops and while loops. I am really stuck as I am a beginner. I have some failed attempts in the ignored texts. My coding sucks. I would really appreciate some guidance with this.
| |
Hi, you are in the right track. Here are some changes I made to your code.
Notice for the dollars etc I change them to float from int as you are using a decimal point. The x,y,z are redundant as you can just use the coke, pepsi and water vars. I changed up your while loops also look over the differences in them.
Should get you going again.
| |
You might want to have a look at this. It would stop serving drinks if there are none left and it will exit if you ask for something that is not "on the menu" / if you provide invalid input.
I expect you will be able to adapt this to your needs.
| |
Pretty good nico.
Here is a bit more of an OOP approach.
inv.h;
| |
Main.cpp
| |
Hi switchy. I modified your code to work on mine with stdafx.h. That is what I need to use. Thank you so much. Except. How would I make it so you have to "enter in" coins or a dollar bill into it? It would need to also give you back your change. Where would I start? Thank you so much for your help. Here is the code.
| |
Hi, maybe have your coins as int like you did before and have it such as quarter = 25. Multiply the users input by 100 and keep looping until the value is less than the current coin. For example
| |
There is likely a more efficient way but that is just what I thought of first.
Last edited on
Using an int to represent money is a very good idea, it will ensure that you don't get any trouble with inaccuracies as you may have when you use a float or double.
As for how to calculate the number of dollars, have a look at the modulo operator.
http://www.cplusplus.com/doc/tutorial/operators/
| |
Hello. Here is what I tried. I still have no idea what I'm doing. I need to touch up on the little C++ I know. At first, I tried making a function to ask for the money. I could not figure out how to make it use the Dinero int from the function in the main. I tried doing pointers, but I failed. I also couldn't figure out how to make it stop with 3 different amounts, since the prices are different. Should I make 3 different functions? I then just added the code to only the Coke for testing purposes. What I tried doing is using a do loop where it will keep on asking to put in more money till the Money is equal or greater than the price. I also couldn't align the {} brackets with the right one. Am I on the right track? How do I fix it? Thank you.
| |
Hello,
You declared the function as: void EnterMoney(arguments);
"void" means: this function will not return anything.
As a result, your compiler should complain at line 100, because there you try to return an integer, which is not nothing.
Try declaring the function as: int EnterMoney(arguments) both in line 79 and line 3.
Note that in this case, the int Dinero arguments becomes useless, you could just declare the Dinero variable inside the function and initialize it to 0 for any situation.
Another way would be to call the function with the Dinero variable "by reference" instead of "by value". Have a lood at these:
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_value.htm
https://www.tutorialspoint.com/cplusplus/cpp_function_call_by_reference.htm
If you send the variable from the main function by reference, any change that is made inside the function will immediately update the variable in the main function, so you would not need to return the Dinero variable anymore.
---------------
As for the function arguments, it seems you never use ", int Nickel, int Dime, int Quarter, int Dollar", so I would remove those. The function declaration would then become:
void EnterMoney (int &Dinero, int Price); or int EnterMoney (int Price);
You provide Price as a pointer, you could just do the same as with the other variables. If you want to change the price, you should calculate the price before you call the function. For example, for a Pepsi you would call it like:
| |
or
int receivedCash = EnterMoney (55);
Topic archived. No new replies allowed.
Write a Function That Will Help a Vending Machine
Source: http://www.cplusplus.com/forum/beginner/199201/
Post a Comment for "Write a Function That Will Help a Vending Machine"