If statements not working?

前端 未结 2 1580
不思量自难忘°
不思量自难忘° 2020-11-27 08:40

I am a noob at programming. I just wanted to ask what is wrong with the following code:

scanf(\"%i\", &battlechoice);

printf(\"BCHOICE WAS:%i\\n\", batt         


        
相关标签:
2条回答
  • 2020-11-27 09:06

    you are writing if(battlechoice=4) correct it with if(battlechoice==4)

    because '=' and '==' operators both are different

    '=' is Assignment Operator and '==' is comparison operator

    see the link for operators in C http://www.tutorialspoint.com/cplusplus/cpp_operators.htm

    0 讨论(0)
  • 2020-11-27 09:09

    You're confusing the assignment operator = with the equals operator ==. Write this instead:

    if (battlechoice == 4)
    

    And so on.

    Some C programmers use "Yoda conditionals" to avoid accidentally using assignment in these cases:

    if (4 == battlechoice)
    

    For example this won't compile, catching the mistake:

    if (4 = battlechoice)
    
    0 讨论(0)
提交回复
热议问题