Click to See Complete Forum and Search --> : Simple Question


rakeshsv
April 3rd, 2003, 04:06 PM
Hi,

I have somehting like this

string str;

str = "5";

I want to convert this str to an integer 5. How can I do this . I am using atoi(str) I am getting compilation errors.

Can anyone plese help me.

thanks
rakesh

pim42
April 3rd, 2003, 04:10 PM
I believe this will work:
atoi(str.c_str());

you are using an STL string and atoi expects a C style char *

PaulWendt
April 3rd, 2003, 04:38 PM
This also will work:

#include <sstream>
int main(int argc, char* argv[])
{
string str = "5";
int i;

stringstream ss(str);
ss >> i;

return 0;
}


Yeah that's a lot of temporary variable creation, huh? Well you're
in luck if you're thinking that! Go to http://www.boost.org.
At their site, look up lexical_cast [part of the conversion library].

As an aside, are you on the Qt mailing list? There was a VERY
similar question posted on there today :)

--Paul