title: C++ Extract int from string using stringstream
body: <p>I am trying to write a short line that gets a string using getline and checks it for an int using stringstream. I am having trouble with how to check if the part of the string being checked is an int. I've looked up how to do this, but most seem to throw exceptions - I need it to keep going until it hits an int. </p>
  
  <p>Later I will adjust to account for a string that doesn't contain any ints, but for now any ideas on how to get past this part?</p>
  
  <p>(For now, I'm just inputting a test string rather than use getline each time.)</p>
  
  <pre><code>int main() {
  
      std::stringstream ss;
      std::string input = "a b c 4 e";
  
      ss.str("");
      ss.clear();
  
      ss &lt;&lt; input;
  
      int found;
      std::string temp = "";
  
      while(!ss.eof()) {
              ss &gt;&gt; temp;
              // if temp not an int
                      ss &gt;&gt; temp; // keep iterating
              } else {
                      found = std::stoi(temp); // convert to int
              }
      }
  
      std::cout &lt;&lt; found &lt;&lt; std::endl;
  
      return 0;
  }
  </code></pre>