from string to int

Hello everyone!!

I'm new in C++ and I would appreciate any idea how to convert a string of digits into integer it represents. For ex. "15" to 15.

Zara
Or use custom function if your want so:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
template <class T>
inline T ToNumber(const string& text)
{
	stringstream ss(text);
	T result;
	ss >> result;

	if (result)
	{
		return result;
	}
	else
	{
		return 0;
	}
}


or use atoi
One possible problem with std::atoi is when it returns 0 you don't know if it failed or not.
that same issue applies to the template function as supplied by codekiddy
Thank you All for help!
Topic archived. No new replies allowed.