Thread: Vending "machine" C++ code help

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Post Vending "machine" C++ code help

    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.

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    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."

  3. #3
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    Post vending machine code

    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

  4. #4
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    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."

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    3

    More Info.

    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

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    139
    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."

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Extended ASCII Characters in an RTF Control
    By JustMax in forum C Programming
    Replies: 18
    Last Post: 04-03-2009, 08:20 PM
  2. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  3. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 10:20 AM
  4. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM