Public Group

# [c++] Basic divide program, can't find the mistake

This topic is 3414 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Basic "enter an amount and it will divide the amount into fiftys, tens, fives and ones" program written in C++ as learning excersise. When you enter 0.01 or 0.02 it works fine, but if you add a number higher than 0.02 it will give problems. I just don't know what I'm doing wrong in the program. I added the "Added X, Left: X" because I was hoping this would give me any clue but no help there.
#include <iostream>

int main(int argc, char* argv[])
{

double amount, total_amount;
int one_e = 0, five_e = 0, ten_e = 0, fifty_e = 0;

std::cout << "Please enter an amount: ";
std::cin >> amount;
total_amount = amount;

std::cout << "\n\n\nTotal amount: " << total_amount;

while(true)
{
if (amount < 0.01)
break;

if(amount >= 0.5)
{amount -= 0.5; fifty_e++;std::cout << "\nAdded fifty. Left: " << amount ; continue;}

if(amount >= 0.1)
{amount -= 0.1; ten_e++;std::cout << "\nAdded ten. Left: " << amount ; continue;}

if(amount >= 0.05)
{amount -= 0.05; five_e++;std::cout << "\nAdded five. Left: " << amount ; continue;}

if(amount >= 0.01)
{amount -= 0.01; one_e++;std::cout << "\nAdded one. Left: " << amount ; continue;}

}

std::cout << "\n\nFifty: " << fifty_e;
std::cout << "\nTen: " << ten_e;
std::cout << "\nFive: " << five_e;
std::cout << "\nOne: " << one_e;

/*** Just to make sure the cmd window doesnt close instantly ***/
int program_pause;
std::cin >> program_pause;

return 0; // Stops the program
}



##### Share on other sites
Define "give problems".

##### Share on other sites
Quote:
 Original post by SiCraneDefine "give problems".

Ow I'm sorry.

If you enter 0.01 it works fine
If you enter 0.02 it works fine
If you enter 0.03 it gives twice the one (0.01) instead of three times so the total value is 0.02 instead of the 0.03.
If you enter something like 2.60 it gives 2.54 in total value if you combine all the fifty's and tens etc instead of 2.60

##### Share on other sites
I just tried to debug it using Visual Studio. The problem seems to be floating point inaccuracy. When you enter 0.7 as input for example, the value of amount is slightly less than 0.7 which causes some problems. Subtracting some very small value from the values you compare with should solve the problem.

Edit:

Ninja'd

##### Share on other sites
Quote:
 Original post by johand_I just tried to debug it using Visual Studio. The problem seems to be floating point inaccuracy. When you enter 0.7 as input for example, the value of amount is slightly less than 0.7 which causes some problems. Subtracting some very small value from the values you compare with should solve the problem.

I don't realy understand where the floating point inaccuracy is caused from, but you are right in the sense that is does work if I enter 0.031 for example.

Still, I find it very strange that I get these problems. Are floating numbers always slightly inaccurate? If so, why using them at all?

And one last question, if there a better way to program this application (in the way that it gives the same result) ?

##### Share on other sites
Multiply the input by 100 and work with integer values.

##### Share on other sites
Quote:
 Are floating numbers always slightly inaccurate?

Yep. For a very detailed description of the problem see:
http://docs.sun.com/source/806-3568/ncg_goldberg.html

In your particular case SiCrane's solution might be somewhat more elegant (no need to subtract an epsilon value when your working with integers).

##### Share on other sites
Oke I did some more research myself on floats.
It is clear to me now for a big part, thanks everyone for the help.

Kind regards,
Rob

1. 1
2. 2
Rutin
19
3. 3
4. 4
frob
13
5. 5

• 9
• 17
• 11
• 9
• 17
• ### Forum Statistics

• Total Topics
632604
• Total Posts
3007369

×