473,657 Members | 2,667 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problems with Code, Cash Register problem

1 New Member
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.
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <iomanip>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. //Prices for items
  7. void priceAmount(float info[])
  8. {
  9.     float price, subtotal, tax, total;
  10.     char select = '0';
  11.  
  12.     cout <<"Please enter the price of your first item: ";
  13.     cin >> subtotal;
  14.  
  15.     do{
  16.         cout << "would you like to purchase another item? (Y or N): ";
  17.         cin >> select;
  18.  
  19.         while (select != 'Y' && select != 'y' && select != 'N' && select !='n')
  20.         {
  21.             cout << "Please enter Y or N:";
  22.             cin >> select; 
  23.         }
  24.  
  25.         if (select == 'Y' || select == 'y')
  26.         {
  27.             cout << "Enter the price of the nest item: ";
  28.             cin >> price;
  29.  
  30.             subtotal = subtotal + price;
  31.         }
  32.     }while(select == 'Y' || select == 'y');
  33.  
  34.     tax = 0.05 * subtotal;
  35.     total = tax + subtotal;
  36.  
  37.     info[1] = subtotal;
  38.     info[2] = tax;
  39.     info[3] = total;
  40.     cout << "Total = " << setprecision(2) << fixed << showpoint << total <<endl;
  41.  
  42.     return;
  43. }
  44.  
  45. //Payment
  46. void paymentAmount(float info[])
  47. {
  48.     float payment = 0.0;
  49.  
  50.     cout << "Enter payment amount: ";
  51.     cin >> payment;
  52.  
  53.     while(payment < info[3])
  54.     {
  55.         cout << "Not enough money, Do dishs, or enter new payment: ";
  56.         cin >> payment;
  57.     }
  58.  
  59.     info[4] = payment;
  60.  
  61.     return;
  62. }
  63.  
  64. //whats the change
  65.  
  66. void changeAmount(float info[])
  67. {
  68.     float change;
  69.     change = info[4] - info[3];
  70.     change = change * 100;
  71.     change = floor(change);
  72.     change = change / 100.0;
  73.     info[5] = change;
  74.  
  75.     return;
  76. }
  77.  
  78. //Change
  79.  
  80. void changeCalc(float info[])
  81. {
  82.     float change = 0.0;
  83.     int amount, dollars, quart, dime, nick, penn;
  84.     amount = dollars = quart = dime = nick = penn = 0;
  85.     change = info[5];
  86.  
  87.     amount = change * 100;
  88.  
  89.     dollars = amount / 100;
  90.     amount = amount % 100;
  91.  
  92.     quart = amount / 25;
  93.     amount = amount % 25;
  94.  
  95.     dime = amount / 10;
  96.     amount = amount % 10;
  97.  
  98.     nick = amount / 5;
  99.     penn = amount % 5;
  100.  
  101.  
  102.  
  103.     info[1] = dollars;
  104.     info[2] = quart;
  105.     info[3] = dime;
  106.     info[4] = nick;
  107.     info[5] = penn;
  108.  
  109.     return;
  110.  
  111. }
  112.  
  113. //display
  114.  
  115. void display(float change[], float purchase[])
  116. {
  117.     cout << left << setw(25) << "Subtotal: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[1] <<endl;
  118.     cout << left << setw(25) << "Tax: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[2] <<endl;
  119.     cout << left << setw(25) << "Total: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[3] <<endl;
  120.     cout << left << setw(25) << "Payment: " << setw(30) << right << setprecision(2) << fixed << showpoint << purchase[4] <<endl << endl;
  121.  
  122.     cout << "Change: " << (int)change[1] << "dollars, " << (int)change[2] << "quarters, " << (int)change[3] <<  "dimes, " << (int)change[4] <<"nickels, " << (int)change[5] << "pennies." << endl << endl;
  123.  
  124.     return;
  125. }
  126.  
  127. int main()
  128. {
  129.     float purchase[6] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  130.     //                subtotal,tax,total,payment,change
  131.  
  132.     priceAmount(purchase);
  133.  
  134.     paymentAmount(purchase);
  135.  
  136.     changeAmount(purchase);
  137.  
  138.     float change[7] = {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0};
  139.     //                dollars,quarters,dimes,nickles,pennies,change
  140.     change[6] = purchase[5];
  141.  
  142.     changeCalc(change);
  143.  
  144.     display(change, purchase);
  145.  
  146.     return 0;
  147.  
  148.  
  149.  
  150.  
  151. }
  152.  
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
Dec 14 '07 #1
2 3338
weaknessforcats
9,208 Recognized Expert Moderator Expert
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.
Dec 17 '07 #2
(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.
Oct 6 '10 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
6331
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
1
4197
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...
7
1866
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...
1
1370
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.
2
3844
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"
1
2639
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...
4
5394
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
4
1878
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
2
3334
Fary4u
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 ?
0
8402
marktang
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...
0
8315
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,...
0
8829
Oralloy
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...
0
8734
jinu1996
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...
1
8508
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,...
1
6172
isladogs
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...
0
5633
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();...
1
2733
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
2
1962
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.