title: Converting string to number when using getline()
body: <p>I've picked up a book on C++ and I'm basically at the very beginning of it (just started). For some of the problems I had to solve within the book I used the input stream cin the following way --></p>
  
  <pre><code>cin &gt;&gt; insterVariableNameHere;
  </code></pre>
  
  <p>But then I did some research and found out the cin can cause a lot of problems, and so found out about the function getline() within the header file sstream.</p>
  
  <p>I'm just having some trouble trying to wrap my head around what's happening in the following code. I don't see anything that uses the extraction operator (>>) to store the number value in. Its (my problem) further explained in the comments I left.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  // Program that allows a user to change the value stored in an element in an array
  
  int main() 
  {
      string input = "";
      const int ARRAY_LENGTH = 5;
      int MyNumbers[ARRAY_LENGTH] = { 0 };
  
      // WHERE THE CONFUSION STARTS
      cout &lt;&lt; "Enter index of the element to be changed: ";
      int nElementIndex = 0;
      while (true) {
          getline(cin, input); // Okay so here its extracting data from the input stream cin and storing it in input
          stringstream myStream(input); // I have no idea whats happening here, probably where it converts string to number
          if (myStream &gt;&gt; nElementIndex) // In no preceding line does it actually extract anything from input and store it in nElementIndex ? 
           break; // Stops the loop
          cout &lt;&lt; "Invalid number, try again" &lt;&lt; endl;
      }
      // WHERE THE CONFUSION ENDS
  
      cout &lt;&lt; "Enter new value for element " &lt;&lt; nElementIndex + 1 &lt;&lt; " at index " &lt;&lt; nElementIndex &lt;&lt; ":";
      cin &gt;&gt; MyNumbers[nElementIndex];
      cout &lt;&lt; "\nThe new value for element " &lt;&lt; nElementIndex + 1 &lt;&lt; " is " &lt;&lt; MyNumbers[nElementIndex] &lt;&lt; "\n";
      cin.get();
  
      return 0;
  }
  </code></pre>