C++ help? Converting apstring to an integer

Discussion in 'Mac Programming' started by jialuolu, Oct 11, 2007.

  1. jialuolu macrumors regular

    Oct 10, 2005
    Basically, I need to convert an apstring to an integer and this is how I thought it was done

    apstring example = "45";
    int number;

    number = atoi(example_c.str());

    But clearly that's not it because it's not working. Any thoughts? Thanks
  2. sord macrumors 6502

    Jun 16, 2004
    How so isn't it working? Compiler error? Returning different number? Crashing?

    I'm not familiar with the apstring class, not sure why the AP academics don't just use string, but that's beside the point.

    EDIT: Also, your variable is example, not example_c. And I think the method in that class is c_str(). Unless you didn't copy and paste, that should be a good hint. The compiler should have given you a pretty straight forward error about that. Learn to use the compiler errors for assistance.
  3. jialuolu thread starter macrumors regular

    Oct 10, 2005
    Whoops yeah sorry, was typing quickly, I meant example.c_str(). The compiler doesn't give me an error, it's just that when I print it, it gives just me some garbage from somewhere in the memory

    This is dumb, it works if I use string and not apstring.. What a trivial thing to get stuck on :(
  4. gnasher729 macrumors P6


    Nov 25, 2005
    Hint: In XCode, switch Active Build Configuration to Debug (that is the default anyway), set a breakpoint on the first line of main (), then Command-Y = Build and Debug. Then have a look where the constructor puts your C string, and what c_str () returns.

Share This Page