Click here to Skip to main content
15,884,537 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C++
void flip ( int i )

{
  assert( i==1 && i==0);
  if(i = 1)
    i=0;
  else
    i=0;
}

int main()
{
  int i = 1;
  assert(i==1);
  flip(i);
  assert(i==0);
  flip(i);
  assert(i==1);
  cout << "Success!\n";
  return 0;
}


[HB: add pre tags]
Posted
Updated 12-Feb-13 18:44pm
v2

The assertion in flip() will always fail because you are trying to assert that i is equal to both 0 and 1. You also change the value of i (locally) to zero regardless of input value.

The call to flip() does not change the value of i in main(). You need to make flip refer to the variable i instead of a copy of its value: ie. "void flip(int* i)".
 
Share this answer
 
Comments
Albert Holguin 13-Feb-13 18:16pm    
Good input... but there are oh so many errors in that code. :)
Couple of things, i think are wrong

C++
assert( i==1 && i==0); // Why &&. I think this should be ||
  if(i = 1)
    i=0;
  else
    i=0;   // Here you will have to set i = 1 (assuming this is what flip function is supposed to do


EDIT: the "=" in your if conditions should be changed to "=="
 
Share this answer
 
v2
Comments
Albert Holguin 13-Feb-13 18:13pm    
You missed the if(i=1) error, that's setting the value to 1 all the time.
Rahul Rajat Singh 13-Feb-13 23:15pm    
OOPs, My bad. I will correct the mistake. Apparently I am spending too much time with sql nowadays and this "=" started seeming ok with if :)
Mas11 13-Feb-13 23:39pm    
Nice Answer ! My 5*

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900