By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,942 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

Number conversion with string streams

P: n/a
I'm working on a program which needs to convert a string to various data
types. I created a function using string streams and templates to do just
that:

template<typename T>
bool StringToNumber(string & s, T & number)
{
istringstream iss(s);

iss >> number;

return !iss.fail();
}

This function works great. It even checks the magnitude of the number in
the string and fails if the string number is too big to fit into the given
datatype.
Here's the problem: It converts chars differently than other types.
Example:
string s = "123";
char num;
StringToNumber(s, num); // num == '1'

How can I get the function to convert "123" into the number 123 not the
character '1' while still checking the magnitude so that a string like "500"
will fail to convert into a char.

Thanks.


Jul 19 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.