I need to create a simulation of a vending machine and need help in creating code that will take a quantity of drink, subtract 1 and make appropriate change.
I need to create a simulation of a vending machine and need help in creating code that will take a quantity of drink, subtract 1 and make appropriate change.
what code do you have right now? and what specific problems do you have in doing this?
"The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."
I don't know exactly where to begin except to create function and the variables but the way the loop would calculate the correct change, subtract one from the drink list file and display the corect change is where I'm finding difficulty. The contents of the file that have the information to read from is:
Cola .75 20
Root Beer .75 20
Lemon-Lime .75 15
Grape Soda .80 20
Bottled water 1.00 25
Cream Soda .95 15
Black Cherry Soda .95 10
amount put in - price of item = change
then just go through the available coin options from largest to smallest
while (change > 0)
{
if change >= .25
quarter++
change-.25
else if change >=.10
dime++
change-.10
else ....
}
for the file you read the whole file in and when done rewrite the file with the new values
Code:ofstream outFile; outFile.open("vending.txt"); outFile<< Drink_name <<' '; outFile << price << ' '; outFile << number << endl; //etc...
"The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."
Lostminds,
Thanks, your help is indeed valuable. I did forget to mention also that this program should create an array of 7 structures, one for each of the drinks. In the end, it should display the drink name, cost and quantity, ask the user for a drink and quantity and then do the calculations and also display the amount of money the machine earned. How different would the code be with this in mind versus what you provided above?
Steve
It would be almost exactly the same just use the structs instead. You still output the same ie: struct.drink_name etc...
if you have any problems just post again
cheers
"The most common form of insanity is a combination of disordered passions and disordered intellect with gradations and variations almost infinite."