I have looked around, problem with range

Problems with using a set range for an input
I want the input to only allow 0.05 to 0.20
0.05 =< pg <=0.20
1
2
3
4
5
6
7
do{
		cout << "percent of gold (0.05 to 0.20):" ;
		cin >> pg;	//stuff
		
	} while (cf <= 0.05 ||  cf >= 0.20);




So this has
#include <cmath>

And is not allowing me to input 0.20 as an input

thanks all help/any help appreciated.

-Djninjanz


Last edited on
That is because with floating point numbers , you can not test for equality.
Floating point numbers are an approximations of a given fraction , for example just like you can not store the full decimal representation of 1/7 = 0.14285714.. , this is recurring after simply 6 digits but with another fraction say 1/983 , it has 982 recurring digits !!! , We can only approximately save it to say 8 digits 1/983 ~ 0.00101729 , It is close but not exactly 1/983.
Similarly 1/5 or 0.20 can not be stored in binary exactly but a very close approximation could be stored.
Floating point rules of thumb Never use or depend upon equality You can however check if two numbers are close enough.
When you can , avoid using floating point numbers
Last edited on
And is not allowing me to input 0.20 as an input
At line 3 you are inputting into variable pg, but at line 5 you check variable cf, which doesn't appear to be set in the loop. Should you be checking pg instead?
dhayden wrote:
at line 5 you check variable cf, which doesn't appear to be set in the loop. Should you be checking pg instead?

+1

Since you don't show us the declarations for cf or pg, we have to assume you've declared them correctly as floats or doubles. If you've declared them as ints, your code is not going to work.
Last edited on
Thanks a lot guys
so I will try using a different data type such as int or double
when I have some time I will make sure i check and close this,
@akn thanks a lot
@dhayden thats right haha thanks.
@abstraction yes i declared them as floats but why wont it work as an "int"



Solved myself.
Last edited on
Topic archived. No new replies allowed.