fork download
  1. #include <iostream>
  2.  
  3. #include <string>
  4.  
  5. #include <sstream>
  6.  
  7. #include <algorithm>
  8.  
  9. using std::cerr;
  10.  
  11. using std::cout;
  12.  
  13. using std::stringstream;
  14.  
  15. using std::string;
  16.  
  17. using std::for_each;
  18.  
  19.  
  20.  
  21. void convert(const string& a_value)
  22.  
  23. {
  24.  
  25. unsigned short i;
  26.  
  27. if (stringstream(a_value) >> i)
  28.  
  29. cout << a_value << " converted to " << i << ".\n";
  30.  
  31. else
  32.  
  33. cerr << a_value << " failed to convert.\n";
  34.  
  35. }
  36.  
  37.  
  38.  
  39. int main()
  40.  
  41. {
  42.  
  43. string inputs[] = { "abc", "10", "999999999999999999999", "-10", "0" };
  44.  
  45. for_each(inputs, inputs + (sizeof(inputs)/sizeof(inputs[0])), convert);
  46.  
  47. return 0;
  48.  
  49. }
  50.  
  51.  
Success #stdin #stdout 0.01s 2816KB
stdin
Standard input is empty
stdout
10 converted to 10.
0 converted to 0.