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;
}