-
October 8th, 2009, 05:19 AM
#1
Vending Machine Simulation Successfully Executed, but Logic Errors?
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;
}
-
October 8th, 2009, 08:30 AM
#2
Re: Vending Machine Simulation Successfully Executed, but Logic Errors?
It does just as you program it to do.
Take a look here:
Code:
int main ()
{
menu();
checkout();
debitcard();
creditcard();
receipt();
return 0;
}
You're executing the checkout() method. An error happens. As you exit the function you don't care about what happened there, you just continue the execution by calling the next function which is debitcard().
Instead, you should check whether there was an error and if so, then stop. Maybe your main should look like this:
Code:
int main()
{
bool success = menu();
if(!success) return -1;
success = checkout();
if(!success) return -2;
success = debitcard();
if(!success) return -3;
success = creditcard();
if(!success) return -4;
success = receipt();
if(!success) return -5;
return 0;
}
-
October 8th, 2009, 08:57 AM
#3
Re: Vending Machine Simulation Successfully Executed, but Logic Errors?
Code:
if (pin == 1234)
{
void receipt();
}
When you call functions, do not put the 'void' there.
-
October 8th, 2009, 12:29 PM
#4
Re: Vending Machine Simulation Successfully Executed, but Logic Errors?
without the 'void' it says identifier not found...
and when i tried what cilu said I get cant convert from void to bool
-
October 8th, 2009, 12:34 PM
#5
Re: Vending Machine Simulation Successfully Executed, but Logic Errors?
Originally Posted by exospire
without the 'void' it says identifier not found...
and when i tried what cilu said I get cant convert from void to bool
Either declare these functions at the top of the file or move their implementation above where you are using them.
-
October 9th, 2009, 01:32 PM
#6
Re: Vending Machine Simulation Successfully Executed, but Logic Errors?
HI, I have tried out your vending machine simulation, but I had to tweak a little. I'm not very experienced at C++ programming and use a DevC++ compiler. I made following small changes
to the main().
int main ()
{
menu();
checkout();
debitcard();
creditcard();
receipt();
cin.ignore();
cin.get();
}
.....And I got a receipt. Maybe the program logic requires that you must tell it not to accept any
further options. Have a good day...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|