I was just wondering, what would be the best way of converting a char variable (made up of numbers only, up to three digits) into an int variable. Help appreiciated
I was just wondering, what would be the best way of converting a char variable (made up of numbers only, up to three digits) into an int variable. Help appreiciated
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
http://faq.cprogramming.com/cgi-bin/...&id=1043284385
Hint:
Also see isdigit().Code:char c; int x; x = c - '0';
dwk
Seek and ye shall find. quaere et invenies.
"Simplicity does not precede complexity, but follows it." -- Alan Perlis
"Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
"The only real mistake is the one from which we learn nothing." -- John Powell
Other boards: DaniWeb, TPS
Unofficial Wiki FAQ: cpwiki.sf.net
My website: http://dwks.theprogrammingsite.com/
Projects: codeform, xuni, atlantis, nort, etc.
A char variable can only hold a single digit. You probably mean a string (or a character array used to store a string).
Use a stringstream (as in the first FAQ linked) or boost::lexical_cast.
This http://cboard.cprogramming.com/showp...32&postcount=7 works nicely. I seem to reference this a lot