just a little problem whit if statement..

hi im trying to create a program that will just take user input and then print
into a text file like this

ex: user input = 10
text files contains then = 00.01, 00.02 until the number reaches 10

ok simple stuff
but im talking about minutes so every 00.60 my if statement should add 0.40 to
the number but it dosent do that

heres the code i wrote:


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
double x;
    double b = 00.60;
    double a =0.4; // ignore this double here!
    cout << "Enter the ending time of the time scale!  ex: 10.0" << endl;
    cin >> x;
    ofstream file1;
    file1.open("timescale.txt");
    for(double i = 0;i<x;){

    file1 << i << endl;

    if(i == b){  // here is the infamous if statement the problem lies in the   
                // i ==b part of if statement i dont get it why its never true!
cout << "mac donalds!" << endl;  // i made some testing...
    i = i+0.4;
    b += 1;}
    else i=i+00.01;} // this here works just fine
    file1.close();
}





every time i run this the .txt just goes like this:

00.60, 00.61...

question 1 == why is this never true if(i == b)? by all logical sense to me it should be true at the moment i need it.
thank you!
Last edited on
why is this never true if(i == b)?
You can't check floating point numbers for equality, you have to check that the difference is less than the floating point accuracy.
thanks kbw it worked almoust perfectly
and i now have new problem


I inputted 10 and it printed in the .txt file 0.60, 1 and 1.60 ,2 but after that it started doing 2.60 , 2.61 ,3 until it reached 10

i only modified the code above this way

1
2
3
4
5
6
7
8
9
10
11
12
13
double b = 0.60;
 double a =0.4;


rest of the code that is not important here.....


if(i > b){
        cout << "mac donalds!" << endl;
    i +=a;
    b += 1;}
    else i+=00.01;
    }
Last edited on
You may have something like this instead:
if( (i-b) < (0.01) )
So it will check their difference, and if it is less different than 0.01 (Depending on your i += 0.01; it will execute that statement.
If you change i += 0.01; also change if( (i-b) < (0.01) ).
Last edited on
thanks EssGeEich it worked better than my code above but if you change it to
 
if( (b-i) < (0.01))


for the first two numbers (0 and 1) it writes to the .txt 0.59,0.99, 1 ... 1.59, 1.99...
and then it starts working proberly 2.59,2.60,3...
i diden't change anything else than the if statement to
1
2
3
4
5
6
7
8
9
10
11
12
13
14
if( (i-b) < (0.01))

I ended fixing it like this
if((b-i) < (0.01)){
        if(i < 2&&(b-i) < (0.01)){
            
            i+=00.01;
            file1 << i << endl;}
            if(i < 2){
    i +=a+00.01;}
    else i +=a;
    b += 1;}
    else i+=00.01;
    }


if there is easier way to fix that same problem please tell me but if not i think i will be just fine with that

Thanks to every one who helped me!!
ok i tested the program and the problem that is explained above came back to haunt me it does the same thing at 18 and 19

user input =20;
and it does like this 18.59 , 18.99 , 19 .... 19.59, 19.99, 20
what should I do to fix it? i dont even know why it does that?
Topic archived. No new replies allowed.