title: Assign a Variable in If statement [C++]
body: <p>I am trying to assign a variable if one variable equals another value.  Here is a simplified version of my problem:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main()
  {
     int a = 5;
     int b;
  
     cout &lt;&lt; "A is this value (before if):  " &lt;&lt; a &lt;&lt; endl;
     cout &lt;&lt; "B is this value (before if):  " &lt;&lt; b &lt;&lt; endl;
  
  
     if (a==5)
     {
         cout &lt;&lt; "A equals five" &lt;&lt; endl;
         int b = 6;
         cout &lt;&lt; "B is this value (if stmt):  " &lt;&lt; b &lt;&lt; endl;
     }
  
  cout &lt;&lt; "B is this value (outside):  " &lt;&lt; b &lt;&lt; endl;
  
     return 0;
  
  }
  </code></pre>
  
  <p>It outputs the following:</p>
  
  <pre><code>A is this value (before if):  5
  B is this value (before if):  0
  A equals five
  B is this value (if stmt):  6
  B is this value (outside):  0
  </code></pre>
  
  <p>Why does variable "b" not stay assigned as 6 once it leaves the if statement?  And is there a better way to assign it? In my actual code I have five variables I compare to a.</p>