0

I'm trying to figure out why this condition is being skipped.. It is a 2d array, it's pretty self explanatory what's being compared. Here's the code first followed by explanation.

if ((reversed[i][j] == true) && (circle[i][j] == 'C'))
{
   reversed[i][j] == false;
}
else if (circle[i][j] == 'C')
   reversed[i][j] == true;

The problem is that it's skipping the else if EVEN when it's true; I've tested using a cout just before the code where I say:

cout<<circle[i][j]; 

and it's showing that it is indeed 'C' this is very weird, I've never seen anything like it. I hope there's something simple I'm missing.

circle is defined as follows:

char **circle;

circle = new char *[SIZE];
for (int i = 0; i < SIZE; i++)
  circle[i] = new char[SIZE];
16
  • 3
    circle should probably be a std::vector<std::string>. Don't burden yourself with manual memory management.
    – chris
    Apr 7, 2014 at 20:10
  • You have an extra ) at the end of your first line.
    – jwodder
    Apr 7, 2014 at 20:12
  • It looks like your assignments aren't actually assignments (== vs =). It probably IS hitting the else if, it just doesn't look like it.
    – Darryl
    Apr 7, 2014 at 20:13
  • By the way, any reasonable compiler will complain about a statement of just reversed[i][j] == false; given the chance.
    – chris
    Apr 7, 2014 at 20:14
  • 1
    Anyway, if you are using g++ or clang, you can get reasonable errors. For example, warning: statement has no effect [-Wunused-value]. Just crank up the warning levels. Apr 7, 2014 at 20:40

1 Answer 1

7

Are you sure that you wanted to write

if ((reversed[i][j] == true) && circle[i][j] == 'C'))
{
  reversed[i][j] == false;
}
else if (circle[i][j] == 'C')
  reversed[i][j] == true;

instead of this?

if ((reversed[i][j] == true) && circle[i][j] == 'C')
{
  reversed[i][j] = false; // assignment here
}
else if (circle[i][j] == 'C')
  reversed[i][j] = true;  // assignment here
2
  • 1
    Typo in the question, I'm sorry about that. I've updated the question
    – Alex
    Apr 7, 2014 at 20:21
  • 1
    @Deadlypinfish: To be explicit, the lines reversed[i][j] == false; and reversed[i][j] == true; don't do anything and will be removed by the compiler. In theory the == comparison is evaluated and since there is no assignment, the value is tossed away. Thus the statements have no effect. Propagating this fact, the if and else if can be removed also (compiler may not generate code at all). Apr 7, 2014 at 21:48

Not the answer you're looking for? Browse other questions tagged or ask your own question.