PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, April 28, 2022

[FIXED] How to fix suggest parentheses around comparison in operand of '=='

 April 28, 2022     c++, compiler-warnings, parentheses, qt-creator, warnings     No comments   

Issue

I have a code like

if (config_atual[6]==config_atual[7]==config_atual[8] || 
    config_atual[1]==config_atual[4]==config_atual[7] || 
    config_atual[2]==config_atual[4]==config_atual[6])
{
   if (config_atual[7]=='X')
       cout << "O Jogador ganhou!" << endl;
   else if (config_atual[7]=='O')
       cout << "O Computador ganhou!" << endl;
}

about a tic-tac-toe game, and whenever I try to compile this line of code, which verifies three of the win conditions (assuming indexes 0,1,2 for first line, 3,4,5 for second and 6,7,8 for last) I get this warning :

suggest parentheses around comparison in operand of '==' [-Wparentheses]

Which I don't understand. What I am doing wrong, config_atual is a char array that contains the present configuration of the playing board.

What does this warning mean, and how can I correct it?


Solution

The expression config_atual[6]==config_atual[7]==config_atual[8] is grouped as (config_atual[6]==config_atual[7])==config_atual[8]. The part in parentheses is either true or false which is implicitly converted to an int type (or the type of config_atual[8] is that's wider than an int) prior to a second comparison; i.e. config_atual[8]=={1, 0}, where I've used what's in {} to indicate the possibilities. That probably ends up as false.

Occasionally the chaining of == in this manner is useful. But in your case it isn't, and you need to write the expression in a different way;

config_atual[6]==config_atual[7] && config_atual[7]==config_atual[8]

is one such way.



Answered By - Bathsheba
Answer Checked By - Senaida (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing