title: Scope of string in a function
body: <p>The following is a section of code which I'm currently having problems with. My function (named getGuess) needs to be able to take several guesses inputed independently (I've simplified this section of code so that 5 guesses are inputed) and form a single string output of it (named prevGuesses). There is a restriction on the guesses, in that they must be lower case characters and cannot be repeated (this restriction parameter is set in the while loop inside the function prevGuesses). Currently the prevGuesses string's scope is local and unable to hold the guess chars that are supposed to be added to it every loop of the for-loop in the int main function. You can ignore the begin part of my int main function's code where an input called phrase is required.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  char getGuess(string prevGuesses);
  
  int main(){
  
      char guess;
      string phrase, unsolved, prevGuesses;
      cout &lt;&lt; "Enter phrase: ";
      getline(cin,phrase);
  
      for (int i=0; i&lt;5; i++){   // i&lt;5 is an arbitrary parameter designed for 5 guesses
          getGuess(prevGuesses);
          cout &lt;&lt; prevGuesses &lt;&lt; endl; //this serves as a check for prevGuesses string
                                       //currently blank for every output
      }
      cout &lt;&lt; prevGuesses &lt;&lt; endl; //also blank 
                                   //suppose to be a string of the 5 guessed characters 
  }
  
  char getGuess(string prevGuesses){
  
      char guess;
      cout &lt;&lt; "Enter a guess: ";
      cin &gt;&gt; guess; 
      while ((guess &lt; 'a') || (guess &gt; 'z') || (prevGuesses.find(guess, 0) 
      &lt;= prevGuesses.size())){                     
          cout &lt;&lt; "Invalid guess! Please re-enter a guess: ";
          cin &gt;&gt; guess;
      }
      prevGuesses = prevGuesses + guess;
      cout &lt;&lt; prevGuesses &lt;&lt; endl; //every output is currently the single guess character
  
      return guess;
  }
  </code></pre>