fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. using namespace std;
  5. int main(int argc, char *argv[])
  6. {
  7. string line;
  8. while(getline(cin, line )) {
  9. cout << line<<" ---> ";
  10. istringstream is(line);
  11. size_t processed;
  12. string hi;
  13. double hello;
  14. is >> hi;
  15.  
  16. try {
  17. hello = stod(hi,&processed);
  18. cout<<"number:" <<hello;
  19. if (processed<hi.size())
  20. cout << " (followed by something)";
  21. cout <<endl;
  22. }
  23. catch (...)
  24. {
  25. cout <<"string: "<< hi << endl;
  26. }
  27.  
  28. }
  29. cout << "Done!" << endl;
  30. }
  31.  
Success #stdin #stdout 0s 4280KB
stdin
123
123abc456
-123
+123
xyz
/123
++123
+ 123
++++123abc234
stdout
123 ---> number:123
123abc456 ---> number:123 (followed by something)
-123 ---> number:-123
+123 ---> number:123
xyz ---> string: xyz
/123 ---> string: /123
++123 ---> string: ++123
+ 123 ---> string: +
++++123abc234 ---> string: ++++123abc234
Done!