Jump to content

  • Log In with Google      Sign In   
  • Create Account

#ActualServant of the Lord

Posted 14 March 2013 - 02:59 PM

If you're using C++11, you can use std::to_string() to convert most basic types to a string, and std::stoi() (string to int) std::stof() (string to float), and related functions.

 

Are you really needing to check the output? If so, returning a bool and taking the result as a parameter is a good option.

 

You could also return a constant for invalid input: (std::numeric_limits<int>::max() or similar for ints, and NaN for floats). With NaN, this is a good option, but with integers, you'll have to be sure to check the result before doing any operations.

 

You might also want to balidate and convert the input as soon as possible, to isolate invalid input at the doorways to your program.


#3Servant of the Lord

Posted 14 March 2013 - 02:56 PM

If you're using C++11, you can use std::to_string() to convert most basic types to a string, and std::stoi() (string to int) std::stof() (string to float), and related functions.

 

Are you really needing to check the output? If so, returning a bool and taking the result as a parameter is a good option.

 

You could also return a constant for invalid input: (std::numeric_limits<int>::max() or float's NaN).


#2Servant of the Lord

Posted 14 March 2013 - 02:53 PM

If you're using C++11, you can use std::to_string() to convert most basic types to a string, and std::stoi() (string to int) std::stof() (string to float), and related functions.


#1Servant of the Lord

Posted 14 March 2013 - 02:52 PM

If you're using C++11, you can use std::to_string() to convert most basic types to a string, and std::stoi() (string to int) std::stof() (string to float), and related functions.


PARTNERS