The recently made a change to my code that made it work correctly. I would like someone to help me understand why it works, but it did not work before.

Before (not working correctly)

total = (amountPaid - purchaseAmount) * 100;

totalInt = total //changes from float to int

totalDollars = totalInt / 100;

After (working)

total = amountPaid - purchaseAmount;

totalInt = total * 100; //changes from float to int

totalDollars = totalInt / 100;

My actual program (cash register)

#include <iostream>
using namespace std;


int main ()

{

float purchaseAmount;

float amountPaid;

int pennies;

int nickels;

int dimes;

int quarters;

int dollars;

int totalDollars;

int changeLeftOver;

int changeLeftOver1;

int changeLeftOver2;

int changeLeftOver3;

int totalDimes;

int totalQuarters;

int totalNickels;

int totalPennies;

float total;

int totalInt;


cout <<" How much did your item cost? ";

cin >> purchaseAmount;

cout <<"How much are you paying?" ;

cin >> amountPaid;

total = amountPaid - purchaseAmount;

totalInt = total * 100;

totalDollars = totalInt / 100;

changeLeftOver = totalInt %100;

totalQuarters = changeLeftOver /25;

changeLeftOver1 = changeLeftOver %25;

totalDimes = changeLeftOver1 /10;

changeLeftOver2 = changeLeftOver1 %10;

totalNickels = changeLeftOver2 /5;

totalPennies = changeLeftOver2 %5;

cout << " Your change is " << totalDollars << " dollars " <<endl;

cout << totalQuarters << " quarters " <<endl;

cout << totalDimes << " dimes " <<endl;

cout << totalNickels << " nickels " <<endl;

cout << totalPennies << " pennies " <<endl;

system ("pause");

return 0;

}

//End main

Recommended Answers

All 2 Replies

My reason for thinking the before should work is that totalInt is an intiger. When i said totalInt = total, that should have made total (which is float) to change to an int.

First off please use code tags.

Second, actually, this is quite a common and slightly difficult problem, and is 100% down to how numbers are represented on a computer.

First off float is an inaccurate number, it cannot represent even simple number, like 0.2 without some rounding error, that is because it is a set of binary values, e.g. to represent say 0.25 , we say that the number (in binary) is 001 and there are two shifts of the decimal point, e.g. the number read from RIGHT to LEFT: units, 1/2, 1/4, 1/8 etc... However there is no non-infinite sequence that represents many simple decimals, e.g. 0.2. [This is a gross simplification, see e.g. http://en.wikipedia.org/wiki/IEEE_754-2008 ]

The code you gave does this: total = (A-B)*100; where A and B are floating point numbers. Then you multiply but 100. What you have done in increase the error by a factor 100. [Note in binary that is a shift of over 6 bits.]

Then you have the other problem, you convert your floating point number to an integer like this : int I=A; . Now what is important is now that this simple cuts off all the decimal part, e.g. 2.999999 is converted to 2.

Then you say you got it working with the new code, actually, you just reduced the error and got luckier. The code you have is STILL in error, just that there are LESS cases. You will get errors for sums like 100.53-100.00.

So DON'T use floating point number for money. If you must then use a approximate convertions e.g. int nearestInt= F+0.5; , but you then have to be careful about negative numbers, zero etc.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.