I am tryign to make this code for a class i have and i can get it to compile and what not but it doesnt post the right change. Heres is my code, Please help. Its very basic and i think i am just confusing myself. -
#include <iostream>
-
#include <iomanip>
-
#include <cmath>
-
using namespace std;
-
-
//Prices for items
-
void priceAmount(float info[])
-
{
-
float price, subtotal, tax, total;
-
char select = '0';
-
-
cout <<"Please enter the price of your first item: ";
-
cin >> subtotal;
-
-
do{
-
cout << "would you like to purchase another item? (Y or N): ";
-
cin >> select;
-
-
while (select != 'Y' && select != 'y' && select != 'N' && select !='n')
-
{
-
cout << "Please enter Y or N:";
-
cin >> select;
-
}
-
-
if (select == 'Y' || select == 'y')
-
{
-
cout << "Enter the price of the nest item: ";
-
cin >> price;
-
-
subtotal = subtotal + price;
-
}
-
}while(select == 'Y' || select == 'y');
-
-
tax = 0.05 * subtotal;
-
total = tax + subtotal;
-
-
info[1] = subtotal;
-
info[2] = tax;
-
info[3] = total;
-
cout << "Total = " << setprecision(2) << fixed << showpoint << total <<endl;
-
-
return;
-
}
-
-
//Payment
-
void paymentAmount(float info[])
-
{
-
float payment = 0.0;
-
-
cout << "Enter payment amount: ";
-
cin >> payment;
-
-
while(payment < info[3])
-
{
-
cout << "Not enough money, Do dishs, or enter new payment: ";
-
cin >> payment;
-
}
-
-
info[4] = payment;
-
-
return;
-
}
-
-
//whats the change
-
-
void changeAmount(float info[])
-
{
-
float change;
-
change = info[4] - info[3];
-
change = change * 100;
-
change = floor(change);
-
change = change / 100.0;
-
info[5] = change;
-
-
return;
-
}
-
-
//Change
-
-
void changeCalc(float info[])
-
{
-
float change = 0.0;
-
int amount, dollars, quart, dime, nick, penn;
-
amount = dollars = quart = dime = nick = penn = 0;
-
change = info[5];
-
-
amount = change * 100;
-
-
dollars = amount / 100;
-
amount = amount % 100;
-
-
quart = amount / 25;
-
amount = amount % 25;
-
-
dime = amount / 10;
-
amount = amount % 10;
-
-
nick = amount / 5;
-
penn = amount % 5;
-
-
-
-
info[1] = dollars;
-
info[2] = quart;
-
info[3] = dime;
-
info[4] = nick;
-
info[5] = penn;
-
-
return;
-
-
}
-
-
//display
-
-
void display(float change[], float purchase[])
-
{
-
cout << left << setw(25) << "Subtotal: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[1] <<endl;
-
cout << left << setw(25) << "Tax: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[2] <<endl;
-
cout << left << setw(25) << "Total: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[3] <<endl;
-
cout << left << setw(25) << "Payment: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[4] <<endl << endl;
-
-
cout << "Change: " << (int)change[1] << "dollars, " << (int)change[2] << "quarters, " << (int)change[3] << "dimes, " << (int)change[4] <<"nickels, " << (int)change[5] << "pennies." << endl << endl;
-
-
return;
-
}
-
-
int main()
-
{
-
float purchase[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
-
// subtotal,tax,total,payment,change
-
-
priceAmount(purchase);
-
-
paymentAmount(purchase);
-
-
changeAmount(purchase);
-
-
float change[7] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
-
// dollars,quarters,dimes,nickles,pennies,change
-
change[6] = purchase[5];
-
-
changeCalc(change);
-
-
display(change, purchase);
-
-
return 0;
-
-
-
-
-
}
-
When i go through the program it always ends up just displaying 0dollars, 0 quarters, 0dimes, 0nickles, 0pennies. I dont know where i messed up my code
2 3338
The fundamental error is in using floating point for finance. Floating point is meant for scientific research and not for money. That decimal has trapped more than one beginner.
Use an int and keep your money in pennies.
(1) Lines 85 and 140 are not consistent.
(2) As noted previously, floating point introduces rounding issues, that may or may not be to your advantage.
C++ supports classes and one can identify the data member(s) of an object in a way that does not employ a (confusing/meaningless) array index.
Alternatively one can pass the "purchase" value as a separate argument. From a programming stand point it is always wise to have an array only represent closely related data; arrays are relatively primitive data structures.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Ryan Q. |
last post by:
It works, i'm just posting it hoping someone could do better.
print ''
print ' *** CASH REGISTER ***'
print ' PRESS 0 AND THEN ENTER TO TOTAL'
b = 1
t = 0
v = 0
while b != 0:
a = 1
|
by: Earl Anderson |
last post by:
My brother is in the process of purchasing a neighborhood dry cleaners
store. Having seen some of the process applications I've written in MS
Access, he asked me if I could develop an application to use in his new dry
cleaning store since the existing one is of 1988 vintage. I told him that
although I thought the 'process' involved in a dry cleaners couldn't be too
complex and probably could be developed in Access, I had absolutely no...
|
by: Mr. Mountain |
last post by:
In the following code I simulate work being done on different threads by
sleeping a couple methods for about 40 ms. However, some of these methods
that should finish in about 40 -80 ms take as long as 2300 ms to complete.
This is fairly rare, but the test code below will definitely show it.
Somehow, I must not have my design right.
The idea of this code is that I do two different types of processing (
1-starting and 2-ending) based on...
|
by: Michael Kolias |
last post by:
Hi everybody,
I am having a problem getting the selected value of a drop down list that is
populated dynamically inside a datagrid control. When I try to access the
selected item on the datagrid_Update function I get an Object reference not
set to an instance of an object.
I am reakky stuck here and I would appreciate any help from you guys.
|
by: wahid_kalo |
last post by:
Hi There
I Have Finished A Point Of Sale Program On Access, I Still Need To
ControlThe Cash Register Drawer By Code.
In The Manual It Is Mentionned
To Open The Drawer through com1 under qbasic
OPEN "COM1:300,N8,1"
FOR RANDOM AS #1
PRINT #1,"0000000000"
| |
by: Gen |
last post by:
Hello there,
I am a leader of a team, developing business application based on the
ASP.NET platform. Now we need to add cash register support to the
application and it seems to be a bit of a problem. In general communication
with the cash register consists of two steps:
1 - Creation of a text file, describing the items, quantities and prices;
2 - Invoke cash register's driver (which is an exe file in most cases),
passing the text file...
|
by: ARC |
last post by:
I have a user asking if I could put in a code that will open a register
drawer. My understanding is the receipt printer will normally send the code
the cash drawer. Is there a vba function that someone can share that will
open a drawer without printing? Or does it depend on the drawer? I suppose
if it's just a sequence of characters, then it could be made customizable to
work for many different users. Any ideas?
Thanks!
Andy
|
by: soty |
last post by:
HI
i wrote a code to validate cash for a vending machine.... the problem am having is dat dat my code failed to validate any ammount.
please help!!!!
the code is below
|
by: Fary4u |
last post by:
Hi
i'm trying to develop a software in VB for Cash Register - Till Operator
the only problem is send amount using serial port that can display the amount to pay.
i've got this machine
Epos Touch Screen Terminal & cash register till Dulwich Software
for last 3 day i can't find what should i do any help or any body know any coding which i can use in VB ?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| | |