CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Simple Question

  1. #1
    Join Date
    Nov 2001
    Posts
    36

    Simple Question

    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

  2. #2
    Join Date
    Jan 2003
    Posts
    159
    I believe this will work:
    atoi(str.c_str());

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

  3. #3
    Join Date
    May 2000
    Location
    Phoenix, AZ [USA]
    Posts
    1,347
    This also will work:
    Code:
    #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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured