Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / March 2008



Tip: Looking for answers? Try searching our database.

How can i get an integer from a string using C++?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Can I Get a Word In - 22 Mar 2008 03:35 GMT
If I have a string such as "1.0" as a std::string, how can I convert
that into a float or integer?
David Phillip Oster - 22 Mar 2008 06:38 GMT
> If I have a string such as "1.0" as a std::string, how can I convert
> that into a float or integer?

Wrap the string in a istringstream, and do normal steam i/o on it.

Example:

#include <iostream>
#include <sstream>

int main (int argc, char * const argv[]) {
 std::string s("3.14159");
 float f;
 std::istringstream is(s);
 is >> f;
 std::cout  << f << "\n";
 return 0;
}
Can I Get a Word In - 22 Mar 2008 07:55 GMT
>> If I have a string such as "1.0" as a std::string, how can I convert
>> that into a float or integer?
[quoted text clipped - 14 lines]
>   return 0;
> }
Thanks, I got it.
David Phillip Oster - 22 Mar 2008 16:42 GMT
> >> If I have a string such as "1.0" as a std::string, how can I convert
> >> that into a float or integer?
[quoted text clipped - 15 lines]
> > }
> Thanks, I got it.

The silly way to do it is: extract a C string from the C++ string, then
you could use a C way of doing it like atof() or sscanf(), or you can
get even sillier: convert the C string to an NSString, wrap that in an
NSScanner, and have it parse the string.

#include <iostream>
#include <Cocoa/Cocoa.h>

int main (int argc, char * const argv[]) {

 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
 std::string s("3.14159");
 const char *sc = s.c_str();
 NSString *nsc = [NSString stringWithUTF8String:sc];
 NSScanner *scanner = [NSScanner scannerWithString:nsc];
 float f;
 if([scanner scanFloat:&f]){
   std::cout  << f << "\n";
 }
 [pool release];
 return 0;
}
Can I Get a Word In - 24 Mar 2008 00:05 GMT
>>>> If I have a string such as "1.0" as a std::string, how can I convert
>>>> that into a float or integer?
[quoted text clipped - 37 lines]
>   return 0;
> }
Thanks, but I needed to use a pure C++ solution.
Ben Artin - 24 Mar 2008 15:59 GMT
> Thanks, but I needed to use a pure C++ solution.

If you are using boost, then

#include <boost/lexical_cast.hpp>

string s;
int x = lexical_cast<int>(s);

and similarly for a float.

Ben

Signature

If this message helped you, consider buying an item
from my wish list: <http://artins.org/ben/wishlist>

John C. Randolph - 24 Mar 2008 11:55 GMT
>   NSString *nsc = [NSString stringWithUTF8String:sc];
>   NSScanner *scanner = [NSScanner scannerWithString:nsc];

Once you have an NSString, you can just send it a -floatValue message.  
No need to instantiate an NSScanner.

-jcr
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.