title: i want to take out words from a string sentence and get this error in c++
body: <pre><code>#include&lt;iostream&gt;
  #include&lt;string&gt;
  using namespace std;
  
  void extractFirstWord(string&amp; sentence, string&amp; word);
  void processBlanks(string&amp; sentence);
  
  int main() {
      string sentence, word;
      cout &lt;&lt; "Input a sentence: ";
      getline(cin, sentence);
  
      while (sentence != "") {
            processBlanks(sentence); // removes all blanks from the front of sentence
            if (sentence.length() &gt; 0) { // removing blanks may have made sentence null - cannot extract from a null string
               extractFirstWord(sentence, word); // gets first word from sentence and puts into word
               cout &lt;&lt; word &lt;&lt; endl; // output one word at a time
            }
      }
  
      system("PAUSE");
      return 0;
  }
  
  void extractFirstWord(string&amp; sentence, string&amp; word)
      {
          int i=0;
          while(sentence[i]!=' ')
          {
              i++;        
          }
          word=sentence.substr(0,i);
          sentence=sentence.substr(i);
  }
  // extractFirstWord removes the substring of sentence 
  // from the beginning to the first space from sentence 
  // and stores the same string into word. sentence is
  // shortened accordingly.
  // Postcondition: sentence is shortened, and word
  //                is appropriately the first word in
  //                sentence.
  
  void processBlanks(string&amp; sentence)
      {
          int i=0;
          while(sentence[i]==' '){i++;}
          sentence=sentence.substr(i);
      }
  </code></pre>
  
  <p>processBlanks will remove all of the spaces in front of sentence. Postcondition: sentence has no spaces in front of the first word.</p>
  
  <p>i want to take out words from a string sentence and get this error in c++</p>
  
  <p>Error is -> String subscript out of range</p>