I found this code online as a template to execute a string for float / int / double conversion. This is only here, so I have something to link to the question ....
I want the user to enter a number as a string, convert it to a float, check it for success and drop out if the entry was "Q" or "Invalid input" if it was not "Q", uit and return for more input.
What is the syntax for a test with a conversion error? Will it be ss.fail ()?
// using stringstream constructors. #include <iostream> #include <sstream> using namespace std; int main () { int val; stringstream ss (stringstream::in | stringstream::out); ss << "120 42 377 6 5 2000"; /* Would I insert an if(ss.fail()) { // Deal with conversion error } } in here?! */ for (int n=0; n<6; n++) { ss >> val; cout << val*2 << endl; } return 0; }
Chef flambe
source share