title: How to check if a string can be converted to double in C++?
body: <p>I have a string which can be a number (even a float or double type, not only integer), and it can be also a word which is non-numeric.</p>
  
  <p>I would like to check if this string can be converted into double, and if so, then I would like to do the conversion. In case of a non-numeric string, I want different behaviour.</p>
  
  <p>I have tried this:</p>
  
  <pre><code>double tmp;
  string str;
  stringstream ss;
  
  ss &lt;&lt; str;
  ss &gt;&gt; tmp;
  
  if (ss.fail())
  {
      // non-numeric string
  }
  else
  {
      // string to double conversion is successful
  }
  </code></pre>
  
  <p>The problem with this code is that <code>ss.fail()</code> is always <code>true</code>, even if <code>tmp</code> contains the right value.</p>
  
  <p>There is a function called <code>atof()</code> which converts string to double, but this is not suitable for me, becouse it returns <code>0.0</code> value if the input string is non-numeric. This way I cannot make difference between a non-numeric and a zero input value.</p>