How to convert a string to an int in C++? (Or to a float, double, etc)

Ok, so I already have a post on How to convert an int to a string in C++? (Also doubles, floats, etc…) and so I need a post on doing this in reverse.

So lets say you wanted to convert a string to an int. The code for a function would be simple: (Note updated to one that didn’t give errors on either windows or Linux)

int stringToInt( string inString )
{
	int retVal;
	stringstream ss;
	ss << inString;
	ss >> retVal;
	return retVal;
}

Now, you are going to want a function that handles more than just int types. You want it to handle int, float, doubles, etc… So lets use a template for this as well. However, since the return type is never going to be known, we will not have a return type. Instead, we will pass in a reference to a variable and that will be a generic.

template <class T>
void stringToAnyType(T& t, std::string inString)
{
	std::stringstream ss(inString);
	ss >> t;
}

At first it will appear that this code is all that is needed, and sure the code will work for int, float doubles, but not for 100% for char types because when converting to a char it only gets the first word. Here is a quick example of using the template code. Copy it over and run it with a debugger and look at the variable values.

Note: I am on Windows 7 64 bit (app compiled as 32 bit) using a Visual Studio 2008 C++ empty project. I wonder if the output is different on other platforms.

//#include <iostream>
#include <string>
#include <sstream>

template <class T>
void stringToAnyType(T& t, std::string inString)
{
	std::stringstream ss(inString);
	ss >> t;
}

using namespace std;

int main()
{
	string intAsString = "12345";
	int i;
	stringToAnyType(i, intAsString);

	string floatAsString1 = "12345.678";
	float f1;
	stringToAnyType(f1, floatAsString1);
	// Looks like this works

	string floatAsString2 = "99.99";
	float f2;
	stringToAnyType(f2, floatAsString2);
	// Hmmm...a little loss of precision: 99.989998

	string doubleAsString1 = "123456789.12345678";
	double d1;
	stringToAnyType(d1, doubleAsString1);
	// Perfect conversion

	string doubleAsString2 = "99.99";
	double d2;
	stringToAnyType(d2, doubleAsString2);
	// Hmmm...a little loss of precision: 99.989999999999995


	string charArrayAsString1 = "Hello";
	char c1[100];
	stringToAnyType(c1, charArrayAsString1);

	string charArrayAsString2 = "Hello world";
	char c2[100];
	stringToAnyType(c2, charArrayAsString2);
	// Ahhh! This didn't work.  Only the first word "Hello" is copied into c2

	string trueBoolAsString = "True";
	bool bTrue;
	stringToAnyType(bTrue, trueBoolAsString);
	// Did this really work?

	string falseBoolAsString = "false";
	bool bFalse;
	stringToAnyType(bFalse, falseBoolAsString);
	// Ahhh! This didn't work.  It is still true not false;

	string yesBoolAsString = "Yes";
	bool bYes;
	stringToAnyType(bTrue, yesBoolAsString);
	// Ahhh! This didn't work.  It is still true not false;

	string noBoolAsString = "NO";
	bool bNo;
	stringToAnyType(bFalse, noBoolAsString);
	// Ahhh! This didn't work.  It is still true not false;
}

So obviously there must be a better way that will also get bool conversions and to get the full string when converting a string to a char array, but for now this gets your basic number types and any string without characters such as a space. When I have a solution that works for char types as well, I will let you know.

Also, as you can see that there is a chance of a loss of precision with floats and doubles so be careful.

Leave a Reply

How to post code in comments?