C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Number conversion with string streams

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Eugene
Guest





PostPosted: Wed Jul 30, 2003 10:58 pm    Post subject: Number conversion with string streams Reply with quote



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.




Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.