Vending Machine Simulation Successfully Executed, but Logic Errors? | Newbie | | Join Date: Oct 2009
Posts: 1
| |
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?
:( -
#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;
-
}
|  | Administrator | | Join Date: Feb 2006 Location: South West UK
Posts: 6,210
| | | re: Vending Machine Simulation Successfully Executed, but Logic Errors?
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.
|  | Expert | | Join Date: Feb 2007
Posts: 1,737
| | | re: Vending Machine Simulation Successfully Executed, but Logic Errors? - double chips, milk, cola, coffee, total, zipcode, quantity_chips,
-
quantity_milk, quantity_cola, quantity_coffee, payment, pin, error, tax,
-
final_cost;
Why are all of your variables except for option of type double?
| | Moderator | | Join Date: Mar 2007 Location: North Bend Washington USA
Posts: 5,384
| | | re: Vending Machine Simulation Successfully Executed, but Logic Errors?
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.
|  | | | | Forums
Visit our community forums for general discussions and latest on Bytes
/bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,567 network members.
|