the value of a float variable is going to the wrong if condition.. doesnt matter if thats a 0 or a -1.. it is just going for the condidtion when variable is to +1
-
4can you post code? – GreatJobBob Jan 30 '17 at 12:02
-
5Please post the actual code, not an image of the code – xEric_xD Jan 30 '17 at 12:03
-
2Don't post images of code. Copy-paste the actual code, as text, into the body of the question instead. And [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask) and learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jan 30 '17 at 12:03
-
1The issue is that you're using the assignment operator `=` instead of the comparative operator `==` – xEric_xD Jan 30 '17 at 12:05
4 Answers
You need to use == instead of =.
In c++, assignment operator (=) returns the value equal to the assigned value (this allows writing something like a = b = c). That's why slope = 1 is equal to 1, which, when converted to bool, equals true, and so you end up entering the if section.
- 6,174
- 1
- 26
- 49
Your assigned value in condition, not check it. First you use == instead of =
- 32,079
- 22
- 110
- 197
There is a difference between = and ==. In your if statement you want to check the value, hence you should use ==.
if(slope == 1)
{
/*...*/
}
- 326
- 1
- 5
The following lines in your code are invalid:
if (slope = 0)
if (slope = +1)
if (slope = -1)
This is because you use the assignment operator = instead of the equality operator ==. As a result of this, your if-statements are not making the desired comparison between the slope and the values +1, 0, or -1.
If we have to compare 2 values, 2 variables, or a variable to a value in an if-statement, then we use the equality operator == to compare them over the =. There are a few exceptions to this; see the following page:
Variable assignment in “if” condition.
Just a side note, I would like to point out that you should use some more whitespace in your code; it helps make it more readable. Also, try posting the code itself in your question instead of posting a snapshot of it.
Good luck!
- 1
- 1
- 2,693
- 5
- 17
- 31