c/c++ problem if you know c/c++ please read.

Phoenix

Member
Oct 9, 1999
80
0
66
Hi,

I'm learning c++ got assigned a project, and have written the program. Now the programs all written, compiles nicely no errors (using turbo c++ while at home, and use borland c++ 5.02 at school). Now I run the program input my data and all the answers (data) it produces is correct except for 1 value which is always off by 1. The pennies value is ALWAYS off by 1 in its finaly dispay except if its supposed to be 0. Your comments and assistance would be great.


I am including the source code for the actual function that does the computation below:

----------------------------------------------------------------------------------------------------------------------------------------------------------------
void change(double change_break) // change_break is the actual decimal amount like ex. 0.84 or 1.56
{
int dollars, quarters, dimes, nickels, pennies;
double cents, temp1, temp2;

dollars = change_break / 1;
cents = change_break - dollars;

quarters = cents / .25;
temp1 = quarters * .25;
temp2 = cents - temp1;
cents = temp2;

dimes = cents / .10;
temp1 = dimes * .10;
temp2 = cents - temp1;
cents = temp2;

nickels = cents / .05;
temp1 = nickels * .05;
temp2 = cents - temp1;
cents = temp2;

pennies = cents / .01;
temp1 = pennies * .01;
temp2 = cents - temp1;
cents = temp2;

cout << " Change: " << change_break << endl;
cout << " Dollars: " << dollars << endl;
cout << "Quarters: " << quarters << endl;
cout << " Dimes: " << dimes << endl;
cout << " Nickels: " << nickels << endl;
cout << " Pennies: " << pennies << endl;
}
-----------------------------------------------------------------------------------------------------------------------------------------------------------------
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
You know, accuracy always suffers when using floating point. I had a cursory glance at your code, so I don't know what the problem is. But, if I was making a function like this, I'd modify the code to make it use *integers* only. For example:

void change(double ch)
{
int avb = (int)(ch * 100);
int dl = (int)ch;

avb -= dl * 100;

int q = avb / 25;
avb -= q * 25;

int d = avb / 10;
avb -= d * 10;

int n = avb / 5;
avb -= n * 5;

int p = avb;


cout << " Change : " << ch << endl;
cout << " Dollars : " << dl << endl;
cout << " Quarters: " << q << endl;
cout << " Dimes : " << d << endl;
cout << " Nickels : " << n << endl;
cout << " Pennies : " << p << endl;
}
 

Phoenix

Member
Oct 9, 1999
80
0
66
Thanx for your help, I spoke with a friend like you he said keep it integers. I did that and its STILL off by 1. i'm posting the revised code here but all of it this time, maybe someone can point me to what part is producing the error. the input amounts are bill 3.22 payment is 4.06. All help or suggestions are welcome.

output below:

How much is the bill? 3.22
How much has been tendered? 4.06
Change: 0.84
Dollars: 0
Quarters: 3
Dimes: 0
Nickels: 1
Pennies: 3

source code:

#include <IOSTREAM.H>
#include <CONIO.H>
void change(double);
main(void)
{
double amount_given, amount_bill, amount_change;
cout << "How much is the bill? ";
cin >> amount_bill;
cout << "How much has been tendered? ";
cin >> amount_given;
if (amount_given >= amount_bill)
{
amount_change = amount_given - amount_bill;
change(amount_change);
}
else
{
cout << "You have not given enough money to cover the bill. ";
}
getch();
return 0;
}
void change(double ch)
{
int d, q, di, n, p, con, temp;

con = (int)(ch * 100);

d = con / 100;
temp = con - (d * 100);
con = temp;

q = con / 25;
temp = con - (q * 25);
con = temp;

di = con / 10;
temp = con - (di * 10);
con = temp;

n = con / 5;
temp = con - (n * 5);
con = temp;

p = con / 1;
//temp = con - (p * 1);
//con = temp;

cout << " Change: " << ch << endl;
cout << " Dollars: " << d << endl;
cout << "Quarters: " << q << endl;
cout << " Dimes: " << di << endl;
cout << " Nickels: " << n << endl;
cout << " Pennies: " << p << endl;
}
 

stndn

Golden Member
Mar 10, 2001
1,886
0
0
hate to break my 1000 post, but oh well ... what the hey ... :p

1. why do you always have to assign the remainder of money to temp before con?
eg: temp = con - (di * 10); con = temp;
why not just con = con - (di * 10) ?

2. here's a hint: modulus (%): the remainder after division
eg: 145 % 25 ==> 20

3. that's all .. good luck :)

-1001-
 

Mucman

Diamond Member
Oct 10, 1999
7,246
1
0
I would have mentioned modulus, but I was just assuming they have not learned it yet :)
 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
You still have a floating point problem. In addition you should not use C-style casts in C++. The code "con = (int)(ch * 100);" should use static_cast or reinterpret_cast (I don't use floating point much so I'm not sure of which one is correct). There must be an IEEE float point function that will round to the nearest whole number. I believe on Unix it would be something like: con = ifix(ch * 100.0). Check your system library for the IEEE functions to convert from floating point to integers.
 

Phoenix

Member
Oct 9, 1999
80
0
66
Okay guys first I want to say thankyou for your help and advice.

I have managed to find the FIRST line of code where the error occurs is as follows:

con = ch * 100;

or con = (int) (ch * 100);

this for some ODD reason produces a multipilcation error say the ch is 0.84 so now .84 * 100 = 84 but the compiler is producing 83 WHY? i can't figure out. Now i've tried this several different times with several different variations.

Okay guys it boils down to this for some reason when saving this particular floating point number into a variable declared as integer it rounds down by 1 to 83, now if i left it as floating point (which I did in my original program) it kept it at 84 but introduced the error some time later. suggestions on Y this error is happening.

 

PCHPlayer

Golden Member
Oct 9, 2001
1,053
0
0
This is a very common problem. That is why all those IEEE floating point routines exist. You can fix it by doing the following:
con = reinterpret_cast<int>((ch * 100.0) + .5);
 

Phoenix

Member
Oct 9, 1999
80
0
66
Hey guys,

Thanx for your help again. My Prof. explained today inclass about the problem she felt we should have figured it out, but she explained what happened and why. She said it has to do with the way decimals are used some numbers can't be represented, so that she was expecting us to figure it out and add .005 to our numbers.

Thanx again.
 

thornc

Golden Member
Nov 29, 2000
1,011
0
0


<< Now the programs all written, compiles nicely no errors (using turbo c++ while at home, and use borland c++ 5.02 at school).
>>



A little bit off-topic but here goes, You should get the free borland coomand line compiler and take it home, it's free and it works
great. With that and some programmers editor like CoolEdit or RichEditor you are set to go... win32 programming capable!

Here's a link, it's not from Borland but it should do!