User Tools

Site Tools


c_-_c_streams:string_stream

C - C++ Streams - String Stream

#include <iostream>
#include <sstream>
#include <string>
 
using namespace std;
 
double parse(const string& str) 
{
  stringstream ss(str);
  double d = 0;
 
  ss >> d;
  if (ss.fail( )) 
  {
    throw (str +" is not a number");
  }
 
  return (d);
}
 
 
int main( ) 
{
  try 
  {
    cout << parse("1.234e5") << endl;
    cout << parse("6.02e-2") << endl;
    cout << parse("asdf") << endl;
  }
  catch (string& e) 
  {
    cerr << e << endl;
  }
}
c_-_c_streams/string_stream.txt · Last modified: 2020/07/15 09:30 by 127.0.0.1

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki