Advertisement

does atoi () return zero if the string only contains letters?

Started by January 12, 2003 08:28 AM
5 comments, last by kappa the imp 21 years, 10 months ago
am I right?, the source I got it from was a little dim [edited by - kappa the imp on January 12, 2003 9:32:05 AM]
I don''t know, but you could just test it out. Pass it a string with letters and check the return value.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
Yes, it will return zero. It will read until it finds a non-numeric character. So "123" will return 123, "12kdjd" will return "12", and "alskdjl" will return 0.

thank you
what about "0"? :D
then you would go crazy...

.lick
quote: Original post by Pipo DeClown
what about "0"? :D
then you would go crazy...


how about doing this?
char foo[10] = "0";int intfoo;intfoo = atoi(foo); // if this is wrong sorry, i''m not too sure how it''s calledif(intfoo == 0) {    if(foo[0] == ''0'') {        // intfoo is correct! do something    }    else {        // intfoo is invalid, do error code    }} 

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
For a better way to convert strings to numbers look at the strtol function.
---Ranok---

This topic is closed to new replies.

Advertisement