Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Number conversion with string streams

Reply
Thread Tools

Number conversion with string streams

 
 
Eugene
Guest
Posts: n/a
 
      07-30-2003
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.




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
conversion of real number into binary number rajeswari01 Hardware 1 07-15-2011 05:30 AM
conversion of real number into binary number rajeswari01 Hardware 0 10-15-2010 12:30 AM
Conversion from Hexadecimal number into Decimal number dharmdeep@gmail.com C Programming 14 11-16-2006 07:51 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
String to number conversion Mark Helm XML 0 03-05-2004 05:20 PM



Advertisments