fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. float to_float(const std::string& str)
  6. {
  7. std::istringstream is(str) ;
  8. float result ;
  9.  
  10. is >> result ;
  11. return result ;
  12. }
  13.  
  14. int main()
  15. {
  16. std::string input ;
  17. std::cout << "Enter a number\n" ;
  18. std::cin >> input ;
  19.  
  20. float value = to_float(input) ;
  21. std::cout << "value is " << value << '\n' ;
  22. }
Success #stdin #stdout 0s 3436KB
stdin
3.14159
stdout
Enter a number
value is 3.14159