-9

enter image description here

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

WhiZTiM
  • 21,207
  • 4
  • 43
  • 68
  • 4
    can you post code? – GreatJobBob Jan 30 '17 at 12:02
  • 5
    Please post the actual code, not an image of the code – xEric_xD Jan 30 '17 at 12:03
  • 2
    Don'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
  • 1
    The issue is that you're using the assignment operator `=` instead of the comparative operator `==` – xEric_xD Jan 30 '17 at 12:05

4 Answers4

2

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.

alexeykuzmin0
  • 6,344
  • 2
  • 28
  • 51
2

Your assigned value in condition, not check it. First you use == instead of =

msc
  • 33,420
  • 29
  • 119
  • 214
2

There is a difference between = and ==. In your if statement you want to check the value, hence you should use ==.

if(slope == 1)
{
/*...*/
}
Shaana
  • 326
  • 1
  • 5
0

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!

Community
  • 1
  • 1
BusyProgrammer
  • 2,783
  • 5
  • 18
  • 31