2

This is easy for you. I am busy with a test using c++. I have 2 values:

0 and 1

I can find random number between. Now i want to alternate between them. If i get "1" in step one then in step two I want to get "0" and in step three "1" again where it alternate

step1 = 1
step2 = 0
step3 = 1
step4 = 0

I tried increment and decrements

  int value;
  if(value == 0)
  {   
      value = value + 1;
  }
  else if(value == 1)
  {
      value = value -1;
  }

This code does not work. If it sees a 0 it will become 1 but the next step the value will be 1 again.

Mat
  • 202,337
  • 40
  • 393
  • 406
Johan Dela
  • 85
  • 7
  • You mean that there are series of steps, and in first step if you get 1, then in next step you get zero, and then 1 again, and so on? – SandBag_1996 Aug 23 '12 at 17:33
  • 2
    Aw, you're not initializing `value` :( – jrok Aug 23 '12 at 17:37
  • 1
    What does the "string" from the title refer to? – Max Lybbert Aug 23 '12 at 17:40
  • @jrok Depends on the compiler, some will initialize to zero. – grieve Aug 23 '12 at 17:41
  • It is initialized when i run application with cin >> value; – Johan Dela Aug 23 '12 at 17:41
  • 2
    @grieve, it is officially undefined. Some compilers *do* define the behavior, but it's generally not a good idea to rely on that. It caused a security vulnerability in OpenSLL not too long ago (see also http://blog.llvm.org/2011/05/what-every-c-programmer-should-know.html , http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_14.html , http://blog.llvm.org/2011/05/what-every-c-programmer-should-know_21.html , https://www.securecoding.cert.org/confluence/display/seccode/EXP33-C.+Do+not+reference+uninitialized+memory ) – Max Lybbert Aug 23 '12 at 17:44
  • @MaxLybbert Agreed that you should not rely on it, just pointing out that it may be initialized to zero depending on the compiler you are using. – grieve Aug 23 '12 at 18:01

6 Answers6

3

If you really only ever need 0 and 1:

unsigned int value = 0;

while (1) {
   value = (value + 1) & 1;
   // do whatever is needed with value;
}

This works because in binary 0 + 1 = 1, and 1 + 1 = 10. The '& 1' portion tells us to always take the last bit.

grieve
  • 13,220
  • 10
  • 49
  • 61
1

Wild guess: You are using floating point types (doubleor float) to represent those numbers. You can't compare those types using the == operator, you need to take their precision into account.

Take a look at this question on how to compare floating point types.

Community
  • 1
  • 1
Marcus Riemer
  • 7,244
  • 8
  • 51
  • 76
1

put in a bool switch with your if statements, eg

bool firstHit = false;
if(value == 0 && firstHit == false)
{   
  value = value + 1;
  firstHit = true;
}
else if(value == 1 &&firstHit == true)
{
  value = value -1;
  firstHit = false;
}
Him_Jalpert
  • 2,476
  • 9
  • 31
  • 55
1

Your code is fine dude. I tested it, and its working.

#include <iostream>

using namespace std;

int main ()
{

    int value=1; //Say first step

  for (int i=0 ; i <= 10; i++ )
{
    if(value==0)
    {
        value=value+1;
    }
    else if (value==1)
    {
        value=value-1;
    }
         cout << "Step "<<i<<": "<<value<<"\n";
}

  return 0;
}
SandBag_1996
  • 1,570
  • 3
  • 20
  • 50
1

The code you're showing doesn't initialize value to anything.

Other than that,

if(value == 0)
  {   
      value = value + 1;
  }
  else if(value == 1)
  {
      value = value -1;
  }

This looks like it should work fine if you just want to alternate between 1 & 0, as it will switch it every time you iterate through it.

Sev08
  • 76
  • 7
0

Simplest :

value = 1 - value;

There's a <g> before the colon, but the system software seems to not want to display it.

Pete Becker
  • 74,985
  • 8
  • 76
  • 165