title: how do I use the variable from a For loop outside the loop in C++?
body: <p>So I am a bit rusty in my programming skills and don't have any real world experience except for the college classes I have taken in the past. I am working on a program for a class but running into a snag;I cannot figure out how to use the value of a variable inside a <code>For</code> loop outside of the loop. Here is the code I am referencing:</p>
  
  <pre><code>#include "iostream"
  
  using namespace std;
  
  int want, have, need;
  int counter = 0;
  
  char response, cont;
  
  int diff(int want, int have);
  
  int main(){
  cout &lt;&lt; "Welcome!\n";
  cout &lt;&lt; "This program will help you reach your money saving goals!\n";
  cout &lt;&lt; "Would you like to use this program? (y/n)\n";
      cin &gt;&gt; response;
      while (response == 'y'){
          cout &lt;&lt; "Please enter all amounts in whole dollars only!\n";
          cout &lt;&lt; "Please enter the amount of money you would like to have saved: $";
          cin &gt;&gt; want;
          cout &lt;&lt; "\nPlease enter the amount of money you currently have saved: $";
          cin &gt;&gt; have;
          if (have &gt;= want){
              cout &lt;&lt; "You have already reached or exceeded your goal, this program will not be able to help you!\n";
              system("Pause");
              return 0;
          }
          cout &lt;&lt; "\nYou need to save $" &lt;&lt; diff(want, have) &lt;&lt; " more money to reach your goal!\n";
          cout &lt;&lt; "Would you like me to help you with a savings plan?";
          cin &gt;&gt; cont;
          while (cont == 'y'){
              int menu;
              cout &lt;&lt; "Please select from the following options: \n";
              cout &lt;&lt; "1 - Daily Saving Plan\n";
              cout &lt;&lt; "2 - Weekly Saving Plan\n";
              cout &lt;&lt; "3 - Monthly Saving Plan\n";
              cout &lt;&lt; "Enter the number associated with your choice: \n";
              cin &gt;&gt; menu;
  
              switch (menu){
                  case 1: {
                      int daily;
                      cout &lt;&lt; "You have chosen the Daily Savings Plan\n";
                      cout &lt;&lt; "How much money can you save every day? $";
                      cin &gt;&gt; daily;
                      for (int x = daily; x &lt; need; x++){
                          daily = daily + daily;
                          counter++;
                      }
                      cout &lt;&lt; "\nIt will take you " &lt;&lt; counter &lt;&lt; " days to reach your goal!\n";
                      break;
                  }
                  case 2: {
                      int weekly;
                      cout &lt;&lt; "You have chosen the Weekly Savings Plan\n";
                      cout &lt;&lt; "How much money can you save every week? $";
                      cin &gt;&gt; weekly;
                      for (int x = weekly; x &lt; need; x++){
                          counter++;
                      }
                      cout &lt;&lt; "\nIt will take you " &lt;&lt; counter &lt;&lt; " weeks to meet your goal!\n";
                      break;
                  }
                  case 3: {
                      int monthly;
                      cout &lt;&lt; "You have chosen the Monthly Savings Plan\n";
                      cout &lt;&lt; "How much money can you save every month? $";
                      cin &gt;&gt; monthly;
                      for (int x = monthly; x &lt; need; x++){
                          monthly = monthly + monthly;
                          counter++;
                      }
                      cout &lt;&lt; "\nIt will take you " &lt;&lt; counter &lt;&lt; " months to reach your goal!\n";
                      break;
  
                  }
                  default: cout &lt;&lt; "You made an invalid selection";
  
                      cout &lt;&lt; "Would you like to look at a different saving plan? (y/n)\n";
                      cin &gt;&gt; cont;
              }
          }
      }
  }
  
  int diff(int want, int have){
  return want - have;
  }
  </code></pre>
  
  <p>So, when I run the program, everything runs ok, but the value of counter is always shown as "0" in the final <code>cout</code> statement. </p>
  
  <p>I understand why it is doing this, I think..and it's because of the "int counter = 0" declaration outside the loop, so I assume it goes back to that value after the loop exits. </p>
  
  <p>If I do not initiate the counter variable, I get an error, and if I declare the value inside the loop, I get an error trying to use it in the cout statement as I have above. </p>
  
  <p>I'm not even sure my for loop is structured correctly...basically I want it to add to itself the weekly variable, until the total of <code>x = need</code>. I also want to capture how many iterations it takes for that, then output it as the number of weeks. Hopefully that all makes sense; any and all help is appreciated.</p>