title: Forcing output inside a loop C++
body: <p>I'm coding traffic lights on Raspberry Pi in C++ and ran into a dead-end.</p>
  
  <p>I'm using a functions that's called from the main code block, problem is - it <strong>dumps all intended outputs at the end of the loop.</strong> Both into the console and to physical outputs.</p>
  
  <p>I've searched for a solution, but came up short. </p>
  
  <p>Does anyone know of <strong>a way to force the outputs to happen sequentially</strong> or a place to read up on it?</p>
  
  <pre><code>void idle()
  {
      int allY[]={9,22,13,26};  //output pin numbers
      for(int i=0;i&lt;=10;i++)   // blink X times
      {
          for(int j=0;j&lt;=4;j++)  //all outputs to HIGH
              {
                  digitalWrite (allY[j], HIGH);
                  cout&lt;&lt;"+";   // output to console for testing
              }
  
          usleep(900000); //wait
  
          for (int k=0;k&lt;=4;k++)  // all outputs to LOW
              {
                  digitalWrite (allY[k], LOW);
                  cout&lt;&lt;"-";
              }
      }
  }
  </code></pre>
  
  <p><strong><em>FOR FUTURE READERS - SOLUTION</em></strong></p>
  
  <pre><code>void idle()
  {
      int allY[]={9,22,13,26};//output pin numbers
      for(int i=0;i&lt;=10;i++) // blink X times
      {
          for(int j=0;j&lt;=4;j++)//all outputs to HIGH
              {
                  digitalWrite (allY[j], HIGH);
                  cout&lt;&lt;flush; //flush
              }
  
          usleep(900000); //wait
  
          for (int k=0;k&lt;=4;k++)// all outputs to LOW
              {
                  digitalWrite (allY[k], LOW);
                  cout&lt;&lt;flush;
              }
  
          usleep(900000); //wait
      }
  }
  </code></pre>