Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

jialuolu

macrumors regular
Original poster
Oct 10, 2005
107
0
Canada
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
 
But clearly that's not it because it's not working. Any thoughts? Thanks
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.
 
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.

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 :(
 
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 :(

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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.