C++ help? Converting apstring to an integer

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

  1. jialuolu macrumors regular

    Joined:
    Oct 10, 2005
    Location:
    Canada
    #1
    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

    Joined:
    Jun 16, 2004
    #2
    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

    Joined:
    Oct 10, 2005
    Location:
    Canada
    #3
    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

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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