473,545 Members | 2,047 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Vending Machine Simulation Successfully Executed, but Logic Errors?

1 New Member
Hello everyone, I made a program that simulates a vending machine for class. I finally got it to execute without any errors, yet something is not right. I have functions that are being called upon when they shouldn't be. For instance, I choose my items, and it asks me if I want to pay with 1 debit or 2 credit. If I enter 3 it displays 'Invalid option please try again' at which point the program is supposed to stop, but instead, it goes straight to the creditcard function and says Enter PIN.

Alternatively, if I choose my items, and select 1 for debit payment, it asks me to enter the PIN. The PIN has to be 1234, and if I enter that its suppose to go straight to the receipt function, but instead it again goes to the creditcard function and asks Enter ZIP. I have my code so that if payment option == 1, it goes to the void debitcard function, why is it calling upon the void creditcard function as well?

:(

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <functional>
  3. using namespace std;
  4.  
  5.     int option;
  6.     double chips, milk, cola, coffee, total, zipcode, quantity_chips, 
  7.            quantity_milk, quantity_cola, quantity_coffee, payment, pin, error, tax, final_cost;
  8.  
  9. void menu ()
  10. {
  11.     cout << "Welcome to CSUMB Vending Machine.\n";
  12.  
  13.     do {
  14.         cout << "Select from our menu:\n"
  15.              << "\t1. Sun Chip........$1.50\n"
  16.              << "\t2. Otter Milk......$2.00\n"
  17.              << "\t3. CSUMB Cola......$1.00\n"
  18.              << "\t4. Regular Coffee..$2.50\n"
  19.              << "\t5. Check out\n";
  20.  
  21.         cout << "Enter your option: ";
  22.         cin >> option;
  23.  
  24.             switch (option)
  25.             {
  26.             case 1:
  27.                 cout << "Quantity: ";
  28.                 cin >> quantity_chips;
  29.                 chips = (1.5 * quantity_chips);
  30.                 break;
  31.  
  32.             case 2:
  33.                 cout << "Quantity: ";
  34.                 cin >> quantity_milk;
  35.                 milk = (2 * quantity_milk);
  36.                 break;
  37.  
  38.             case 3:
  39.                 cout << "Quantity: ";
  40.                 cin >> quantity_cola;
  41.                 cola = (1 * quantity_cola);
  42.                 break;
  43.  
  44.             case 4:
  45.                 cout << "Quantity: ";
  46.                 cin >> quantity_coffee;
  47.                 coffee = (2.5 * quantity_coffee);
  48.                 break;
  49.  
  50.             case 5:
  51.                 void checkout();        
  52.                 break;
  53.  
  54.             default:
  55.             cout << "Invalid option." << endl;
  56.  
  57.             }             
  58.     } while (option <5);
  59. }
  60.  
  61. void checkout()
  62. {
  63.     if (quantity_chips > 0||quantity_milk > 0||quantity_cola > 0||quantity_coffee > 0)
  64.     {
  65.         total = (chips) + (milk) + (cola) + (coffee);
  66.  
  67.         if (total > 10)
  68.         { 
  69.             cout << "Error, you are buying too much!\n";
  70.         }
  71.         else
  72.         {
  73.             cout << "Select payment option (1:Debit 2:Credit): ";
  74.             cin >> payment;
  75.  
  76.             if (payment == 1)
  77.             {
  78.                 void debitcard();
  79.             }
  80.             else if (payment == 2)
  81.             {
  82.                 void creditcard();
  83.             }
  84.             else
  85.             {
  86.                 cout << "Unknown payment option, please try again.\n";
  87.             }
  88.         }
  89.     }
  90.     else
  91.     {
  92.         cout << "Please select at least one item.\n";
  93.     }
  94. }
  95.  
  96. void debitcard()
  97. {
  98.     error = 1;
  99.  
  100.     cout << "Enter PIN: ";
  101.     cin >> pin;
  102.  
  103.     if (pin == 1234)
  104.     {
  105.         void receipt();
  106.     }
  107.     else 
  108.     {    
  109.         while (error >= 1)
  110.         {
  111.             cout << "Invalid PIN. Try Again:";
  112.             cin >> pin;
  113.             error++;
  114.             if (pin == 1234)
  115.             {
  116.                 void receipt();
  117.             }
  118.             else
  119.             {
  120.                 cout << "We are sorry but this is an invalid card\n"
  121.                      << "Thank you for using us.\n"; 
  122.                 return;
  123.             }
  124.         }
  125.     }
  126. }
  127.  
  128. void creditcard()
  129. {
  130.     error = 1;
  131.  
  132.     cout << "Enter ZIP";
  133.     cin >> zipcode;
  134.  
  135.     if (zipcode == 93955)
  136.     {
  137.         void receipt();
  138.     }
  139.     else
  140.     {
  141.         while (error >= 1)
  142.         {
  143.             cout << "Invalid ZIP. Try Again: ";
  144.             cin >> zipcode;
  145.             error++;
  146.             if (zipcode == 93955)
  147.             {
  148.                 void receipt();
  149.             }
  150.             else
  151.             {
  152.                 cout << "We are sorry but this is an invalid card\n"
  153.                      << "Thank you for using us.\n"; 
  154.                 return;
  155.             }
  156.         }
  157.     }
  158. }
  159.  
  160. void receipt ()
  161. {
  162.     cout << "This is your receipt:\n";
  163.  
  164.     if (quantity_chips > 0)
  165.     {
  166.         cout << "Sun Chips: $1.50 x " << quantity_chips << " = $" << chips << endl;
  167.     }
  168.     if (quantity_milk > 0)
  169.     {
  170.         cout << "Otter Milk: $2.00 x " << quantity_milk << " = $" << milk << endl;
  171.     }
  172.     if (quantity_cola > 0)
  173.     {
  174.         cout << "CSUMB Cola: 1.00 x " << quantity_cola << " = $" << cola << endl;
  175.     }
  176.     if (quantity_coffee > 0)
  177.     {
  178.         cout << "Regular Coffee: $2.50 x " << quantity_coffee << " = $" << coffee << endl;
  179.     }
  180.  
  181.     tax = (total * .10);
  182.     final_cost = tax + total;
  183.  
  184.     cout << "Tax (10.0%): $" << tax << endl;
  185.     cout << "Total: $" << final_cost << endl;
  186. }
  187.  
  188. int main ()
  189. {
  190.     menu();
  191.     checkout();
  192.     debitcard();
  193.     creditcard();
  194.     receipt();
  195.  
  196.     return 0;
  197. }
Oct 8 '09 #1
3 7604
Banfa
9,065 Recognized Expert Moderator Expert
In main you always call the function creditcard. In fact you always call all the functions. I imagine this is causing or contributing to the problem.
Oct 8 '09 #2
Savage
1,764 Recognized Expert Top Contributor
Expand|Select|Wrap|Line Numbers
  1. double chips, milk, cola, coffee, total, zipcode, quantity_chips, 
  2.            quantity_milk, quantity_cola, quantity_coffee, payment, pin, error, tax,   
  3.            final_cost;
Why are all of your variables except for option of type double?
Oct 8 '09 #3
weaknessforcats
9,208 Recognized Expert Moderator Expert
I would not use floating point for money. Due to rounding, adding and multiplying floating point numbers is not reliable. That is, your answers won't work out.

Use integers. Do your accounting in pennies.

Only when you need to display a number so you need to have a dollar sign and a decimal point. You can put that code inside the display function. That way 1234 would be displayed as $12.34.

This is very easy to do. If your value is in pennies, then value % 100 gets the cents. By subtracting the cents from the value you have the whole dollars in pennies. Just divide the whole dollars by 100 and you are done. Now just display your variables separated, as needed, by a $ and a decimal point.
Oct 8 '09 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

19
7145
by: Nicolas Pernetty | last post by:
Hello, I'm looking for any work/paper/ressource about continuous system simulation using Python or any similar object oriented languages (or even UML theory !). I'm aware of SimPy for discrete event simulation, but I haven't found any work about continuous system. I would like to develop a generic continous system simulator, and so...
1
4019
by: pradeepkumar | last post by:
give me the code for vending machine in c++. it has 10 items in a .first display all the items and its cost and its quantityonhand.then ask the customer if he wishes to buy.if yes tell him to select the item and after that tell him to enter the coins.
0
6030
by: joestevens232 | last post by:
I am seriously stuck and have been working on this for hours and hours and can't figure out my next step....heres the program assignment. This vending machine dispenses 1. M&Ms ($.65), 2. Chips ($1.16), 3. Peppermint gum ($.28). Your program should prompt the user to deposit money (i.e. the user needs to type in the amount deposited at the...
9
3998
by: vpascuzzi | last post by:
Here's the deal: I've been working on this little program forever now, and can't seem to get the final little glitches out of it. I am to build a vending machine, using 2 header .h files (one for the cash register and the other for the dispenser), and 2 member function definitions .cpp (one for each). It is a little lengthy, and there are...
7
14814
by: JohnSmith70 | last post by:
Please help I need help with this homework. Its about vending machines, and it should be a piece of cake to you code experts out there. Its about vending machines. Heres the question: It should have two classes, one for the vending machine(VM) and the other for drinks. The capacity of the VM is specified on creation. The drinks can be...
2
1861
by: nb999 | last post by:
Hello Friends, I was trying to simulate a vending machine using perl just as a fun project. Heres the code I wrote: #!/usr/bin/perl $wt = $ARGV;
1
6981
by: Kunthea | last post by:
I am not that good at programming and I need a little push on how I can start my programming on the vending machine. The basic of the homework is that it displays four snacks and corresponding Labels that indicate numbers for each snack. I need to use a string array that contains the names of each snack. The GUI should contain a TextBox in...
3
2974
by: ismaeel | last post by:
i very need for intrfase of vending machine for netbense sending to email plese
0
7475
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7664
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7436
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7766
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5981
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3463
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3446
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1897
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1022
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.