Bob said:
It isn't quite that easy. For example, what does this program do? You
might expect it to throw an exception, but it doesn't:
Why would I expect that? Apart from memory allocation, where is any
code that might throw?
#include <iostream>
#include <ostream>
#include <sstream>
#include <string>
int main() {
using namespace std;
istringstream ss(string("fish"));
double f = 0.0;
ss >> f;
cout << f << endl;
}
Implicit in my statement that you can use streams is the requirement
that you know (or learn) *how* to use them. What your code is missing
is an important part of using any input stream (including, for example,
std::cin). That is, *every* time you read something, you should check
the stream state to see if it succeeded. So after ss >> f; you need
something like
if (!ss)
{
// failed to read a double
// report the error, perhaps by throwing an exception
}
This is much better IMHO.
For simple cases (like the OP's) it is perfectly sufficient.
Gavin Deane