title: How to get value out of an if statement c++?
body: <p>How can I get a value from an if statement? What I mean is if the number is more then 151 like in the first if statement. How can I get the value of r1 out of that block, so I can add everything at the end including that specific value from what if statement is chosen. </p>
  
  <pre><code>int main (int argc, const char * argv[])
  {
      const double beefMeal = 12.95;
      const double chickenMeal = 10.95;
      const double veganMeal = 8.95;
      int beef, chicken, vegan;
      const int room1 = 250;
      const int room2 = 200;
      const int room3 = 100;
      const int room4 = 50;
      int numParty;
      double r1= 0, r2= 0, r3= 0, r4= 0, b1, c1, v1;
      double grat, total;
  
      cout &lt;&lt;"Enter number of beef meals: ";
      cin&gt;&gt;beef;
      cout&lt;&lt;endl;
      cout &lt;&lt;"Enter number of chicken meals: ";
      cin&gt;&gt;chicken;
      cout&lt;&lt;endl;
      cout &lt;&lt;"Enter number of vegan meals: ";
      cin &gt;&gt; vegan;
      cout&lt;&lt;""&lt;&lt;endl;
  
      cout&lt;&lt;"A Caterers Program"&lt;&lt;endl;
      cout&lt;&lt;""&lt;&lt;endl;
  
      cout.setf (ios::fixed | ios::showpoint);
      cout.precision(2);
      cout&lt;&lt;"Number in party: ";
      cin&gt;&gt; numParty;
      if(numParty&gt;151)
      {
          r1 = (room1 * 6.5)/100;
          cout&lt;&lt;"Room cost:                                           $"&lt;&lt;room1&lt;&lt;endl;
          cout&lt;&lt;"Room tax:                                            $"&lt;&lt;r1&lt;&lt;endl;
      }
      else
      {
          if (numParty &lt;= 150 &amp;&amp; numParty &gt;= 100) 
          {
              r2 = (room2 * 6.5)/100;
              cout&lt;&lt;"Room cost:            $"&lt;&lt;room2&lt;&lt;endl;
              cout&lt;&lt;"Room tax:             $"&lt;&lt;r2&lt;&lt;endl;
          }
          else
          {
              if (numParty &lt;= 99 &amp;&amp; numParty &gt;= 31)
              {
                  r3 = (room3 * 6.5)/100;
                  cout&lt;&lt;"Room cost:            $"&lt;&lt;room3&lt;&lt;endl;
                  cout&lt;&lt;"Room tax:             $"&lt;&lt;r3&lt;&lt;endl;
              }
              else
              {
                  if (numParty &lt;= 30)
                  {
                      r4 = (room4 * 6.5)/100;
                      cout&lt;&lt;"Room cost:            $"&lt;&lt;room4&lt;&lt;endl;
                      cout&lt;&lt;"Room tax:             $"&lt;&lt;r4&lt;&lt;endl;
                  }
              }
          }
      }
  
      cout&lt;&lt;"Number of beef dinners:    "&lt;&lt;beef&lt;&lt;endl;
      b1 = beef * beefMeal; 
      cout&lt;&lt;"Cost of beef dinners:               $"&lt;&lt;b1&lt;&lt;endl;
      cout&lt;&lt;"Number of chicken dinners: "&lt;&lt;chicken&lt;&lt;endl;
      c1 = chicken * chickenMeal; 
      cout&lt;&lt;"Cost of chicken dinners:            $"&lt;&lt;c1&lt;&lt;endl;
      cout&lt;&lt;"Number of vegan dinners:   "&lt;&lt;vegan&lt;&lt;endl;
      v1 = vegan * veganMeal; 
      cout&lt;&lt;"Cost of vegan dinners:              $"&lt;&lt;v1&lt;&lt;endl;
      grat = (b1 + c1 + v1) * 0.18;
      cout&lt;&lt;"Food Gratuity:                      $"&lt;&lt;grat&lt;&lt;endl;
  
      cout&lt;&lt;"________________________________________________"&lt;&lt;endl;
      total = grat + v1 + c1 + b1 + r1 + r2 + r3 + r4;
      cout&lt;&lt;"Total: $"&lt;&lt; total;
  }
  </code></pre>