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;
-
}
3 7595 Banfa 9,065
Expert Mod 8TB
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.
- 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?
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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...
|
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...
|
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;
|
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...
|
by: ismaeel |
last post by:
i very need for intrfase of vending machine for netbense
sending to email plese
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
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,...
| | |