Thread: Vending Machine Simulation Successfully Executed, but still not Right

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    1

    Vending Machine Simulation Successfully Executed, but still not Right

    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?



    Code:
    #include <iostream>
    #include <functional>
    using namespace std;
    
    	int option;
    	double chips, milk, cola, coffee, total, zipcode, quantity_chips, 
    		   quantity_milk, quantity_cola, quantity_coffee, payment, pin, error, tax, final_cost;
    
    void menu ()
    {
    	cout << "Welcome to CSUMB Vending Machine.\n";
    
    	do {
    		cout << "Select from our menu:\n"
    			 << "\t1. Sun Chip........$1.50\n"
    			 << "\t2. Otter Milk......$2.00\n"
    			 << "\t3. CSUMB Cola......$1.00\n"
    			 << "\t4. Regular Coffee..$2.50\n"
    			 << "\t5. Check out\n";
    
    		cout << "Enter your option: ";
    		cin >> option;
    
    			switch (option)
    			{
    			case 1:
    				cout << "Quantity: ";
    				cin >> quantity_chips;
    				chips = (1.5 * quantity_chips);
    				break;
    
    			case 2:
    				cout << "Quantity: ";
    				cin >> quantity_milk;
    				milk = (2 * quantity_milk);
    				break;
    
    			case 3:
    				cout << "Quantity: ";
    				cin >> quantity_cola;
    				cola = (1 * quantity_cola);
    				break;
    
    			case 4:
    				cout << "Quantity: ";
    				cin >> quantity_coffee;
    				coffee = (2.5 * quantity_coffee);
    				break;
    
    			case 5:
    				void checkout();		
    				break;
    
    			default:
                cout << "Invalid option." << endl;
                
    			} 			
    	} while (option <5);
    }
    
    void checkout()
    {
    	if (quantity_chips > 0||quantity_milk > 0||quantity_cola > 0||quantity_coffee > 0)
    	{
    		total = (chips) + (milk) + (cola) + (coffee);
    	
    		if (total > 10)
    		{ 
    			cout << "Error, you are buying too much!\n";
    		}
    		else
    		{
    			cout << "Select payment option (1:Debit 2:Credit): ";
    			cin >> payment;
    
    			if (payment == 1)
    			{
    				void debitcard();
    			}
    			else if (payment == 2)
    			{
    				void creditcard();
    			}
    			else
    			{
    				cout << "Unknown payment option, please try again.\n";
    			}
    		}
    	}
    	else
    	{
    		cout << "Please select at least one item.\n";
    	}
    }
    
    void debitcard()
    {
    	error = 1;
    
    	cout << "Enter PIN: ";
    	cin >> pin;
    
    	if (pin == 1234)
    	{
    		void receipt();
    	}
    	else 
    	{	
    		while (error >= 1)
    		{
    			cout << "Invalid PIN. Try Again:";
    			cin >> pin;
    			error++;
    			if (pin == 1234)
    			{
    				void receipt();
    			}
    			else
    			{
    				cout << "We are sorry but this is an invalid card\n"
    					 << "Thank you for using us.\n"; 
    				return;
    			}
    		}
    	}
    }
    
    void creditcard()
    {
    	error = 1;
    	
    	cout << "Enter ZIP";
    	cin >> zipcode;
    
    	if (zipcode == 93955)
    	{
    		void receipt();
    	}
    	else
    	{
    		while (error >= 1)
    		{
    			cout << "Invalid ZIP. Try Again: ";
    			cin >> zipcode;
    			error++;
    			if (zipcode == 93955)
    			{
    				void receipt();
    			}
    			else
    			{
    				cout << "We are sorry but this is an invalid card\n"
    					 << "Thank you for using us.\n"; 
    				return;
    			}
    		}
    	}
    }
    
    void receipt ()
    {
    	cout << "This is your receipt:\n";
    
    	if (quantity_chips > 0)
    	{
    		cout << "Sun Chips: $1.50 x " << quantity_chips << " = $" << chips << endl;
    	}
    	if (quantity_milk > 0)
    	{
    		cout << "Otter Milk: $2.00 x " << quantity_milk << " = $" << milk << endl;
    	}
    	if (quantity_cola > 0)
    	{
    		cout << "CSUMB Cola: 1.00 x " << quantity_cola << " = $" << cola << endl;
    	}
    	if (quantity_coffee > 0)
    	{
    		cout << "Regular Coffee: $2.50 x " << quantity_coffee << " = $" << coffee << endl;
    	}
    
    	tax = (total * .10);
    	final_cost = tax + total;
    
    	cout << "Tax (10.0%): $" << tax << endl;
    	cout << "Total: $" << final_cost << endl;
    }
    
    int main ()
    {
    	menu();
    	checkout();
    	debitcard();
    	creditcard();
    	receipt();
    	
    	return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Have you looked at your main() function recently?

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    All your functions will be executed because you haven't done anything to avoid this, program execution continues at the line after the original function call once control leaves the function.
    Last edited by rogster001; 10-08-2009 at 05:05 AM. Reason: missed 'void'

  4. #4
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Hey if u r coding in C++ better u can use here class and inheritance kind of which will help u to code close to c++

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    try using some switch case in menu and dont call directly all functions from the main

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by rogster001 View Post
    checkout() is first called from within menu()
    Except, of course, it isn't. The function checkout is declared inside the menu function, but not called.

  7. #7
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I've just had to delete what looks like a spam post from a vending machine site.

    I hope this thread isnt a plot to get us to buy more snack machines and coffee dispensers!!!

  8. #8
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    change your main function to this.

    Code:
    int main ()
    {
    	menu();
    	checkout();
    
    	
    	return 0;
    }
    fixed
    .

    your checkout already calls the debit/credit, and your debit/credit already calls the receipt.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Vending Machine Logic?
    By shwetha_siddu in forum C Programming
    Replies: 10
    Last Post: 04-07-2009, 03:33 AM
  2. School Mini-Project on C/C++ (Need Your Help)..
    By EazTerence in forum C++ Programming
    Replies: 4
    Last Post: 09-08-2005, 01:08 AM
  3. Wanna test vending machine
    By Roaring_Tiger in forum Tech Board
    Replies: 6
    Last Post: 08-25-2004, 05:51 PM
  4. Machine language simulation
    By Nutshell in forum C Programming
    Replies: 5
    Last Post: 06-20-2002, 03:52 PM
  5. Vending machine
    By Unregistered in forum C++ Programming
    Replies: 6
    Last Post: 06-13-2002, 10:21 PM