Help with if and else

I'm writing a code to categorise values and written this code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
	
float E3, Eiii, Ec;
if (E3 >= 20 && E3 < 50) {Eiii = 20; Ec = 50;
	} else if (E3 >= 50 && E3 < 100) {Eiii = 50; Ec = 100;
	} else if (E3 >= 100 && E3 < 120) {Eiii = 100; Ec = Eiii + 20;
	} else if (E3 >= 120 && E3 < 140) {Eiii = 140; Ec = Eiii + 20;
	} else if (E3 >= 140 && E3 < 150) {Eiii = 140; Ec = Eiii + 10;
	} else if (E3 >= 150 && E3 < 160) {Eiii = 150; Ec = Eiii + 10;
	} else if (E3 >= 160 && E3 < 180) {Eiii = 160; Ec = Eiii + 20;
	} else if (E3 >= 180 && E3 < 200) {Eiii = 180; Ec = Eiii + 20;
	} else if (E3 >= 200 && E3 < 220) {Eiii = 200; Ec = Eiii + 20;
	} else if (E3 >= 220 && E3 < 250) {Eiii = 220; Ec = Eiii + 30;
	} else if (E3 >= 250 && E3 <= 300) {Eiii = 250; Ec = Eiii + 50;
	} else { Eiii = 300; Ec = 300; cout << "\nData Out of Range E3\n";}


The output is always Data Out of Range E3. I dont see anything wrong with my code. Can anyone help me with this? Thanks
I'm guessing you haven't set the variables to a certain value. So when you initialize variable E3, it will have a number stored in it from the previous time that certain point in your memory was used.
Unless this isn't your full code...
In your code snip there is no initialization of variable E3. So it has some undefined value that does not belong to the range 20 - 300.
Yes, this is not my full code. The value of E3 is set somewhere else.
Is that value set somewhere in-between line 2 and 3?
No. Its in another file. This is part of the .h file I created.
Maybe these two variables with name E3 are different variables. At least I do not see where this variable has linkage specifier external.
The full code is something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
float log_interpolation (float E1, float E2, float E3) {

... Code...

float Eiii, Ec;
if (E3 >= 20 && E3 < 50) {Eiii = 20; Ec = 50;
} else if (E3 >= 50 && E3 < 100) {Eiii = 50; Ec = 100;
} else if (E3 >= 100 && E3 < 120) {Eiii = 100; Ec = Eiii + 20;
} else if (E3 >= 120 && E3 < 140) {Eiii = 140; Ec = Eiii + 20;
} else if (E3 >= 140 && E3 < 150) {Eiii = 140; Ec = Eiii + 10;
} else if (E3 >= 150 && E3 < 160) {Eiii = 150; Ec = Eiii + 10;
} else if (E3 >= 160 && E3 < 180) {Eiii = 160; Ec = Eiii + 20;
} else if (E3 >= 180 && E3 < 200) {Eiii = 180; Ec = Eiii + 20;
} else if (E3 >= 200 && E3 < 220) {Eiii = 200; Ec = Eiii + 20;
} else if (E3 >= 220 && E3 < 250) {Eiii = 220; Ec = Eiii + 30;
} else if (E3 >= 250 && E3 <= 300) {Eiii = 250; Ec = Eiii + 50;
} else { Eiii = 300; Ec = 300; cout << "\nData Out of Range E3\n";}

}
I do not understand
1) why you do not print the value of E1 in the very beginning of the function that to be sure that it has a given value;
2) why you do not print the value of the corresponding argument before calling the function;
3) why you are disturbing the forum and think that somebody else shall resolve the problem instead of you?!!!
Last edited on
Solved it! Thanks everyone!
Topic archived. No new replies allowed.