title: c++ define variable inside if block for function scope
body: <p>Background: I am working on developing several different controllers (over 10 or so) for a hardware which involves running the code in hard real-time under RTAI linux. I have implemented a class for the hardware with each controller as a separate member function of the class. I'm looking to pass the desired trajectory for the respective control variable to each of these control functions based on which controller is chosen. In addition, since there are several parameters for each controller and I am looking to quickly switch controllers without having to navigate through the entire code and changing parameters, I am looking to define all the control variables at one place and define them based on which controller I choose to run. Here is a minimum working example of what I am looking for.</p>
  
  <p>I am looking to define variables based on if a condition is true or not as follows in C++:</p>
  
  <pre class="lang-cpp prettyprint-override"><code>int foo()
  {
    int i=0;
  
    if(i==0)
    {
      int a=0;
      float b=1;
      double c=10;
    }
    elseif(i==1)
    {
      int e=0;
      float f=1;
      double g=10;
    }
  
  // Memory locked for hard real-time execution
  // execute in hard real-time from here
  
    while(some condition) 
    {
  // 100's of lines of code
      if(i==0)
      {
       a=a+1;
       b=b*2;
       c=c*4;
  // 100's of lines of code
      }
      elseif(i==1)
      {
       e=e*e*e;
       f=f*3;
       g=g*10;
  // 100's of lines of code
      }
  // 100's of lines of code
    }   
  
  // stop execution in hard real-time
  }
  </code></pre>
  
  <p>The above code gives error on execution as the scope of the variables defined in the if blocks is limited to the respective if block. Could anyone suggest a better way of handling this issue? What is the best practice in this context in C++?</p>