Variable changes on it's own in C++

前端 未结 2 795
礼貌的吻别
礼貌的吻别 2021-01-27 14:21

I have a loop going through an array trying to find which index is a string. It should solve for what that value should be. I can\'t figure out why, but as soon as the if statem

2条回答
  •  悲哀的现实
    2021-01-27 14:55

    As has been noted, in your if statements, you are using the assignment operator (=) but want the equality comparison operator (==). For your variable i the first if statement sets i equal to 0 and if(0) is the same as if(false). So your program goes to the first else-if which sets i equal to 1 and if(1) evaluates to true. Your code then finishes the block within else if (i = 1) {...} and then ends.

提交回复
热议问题