Login Register


Craps Game filter_list
Author
Message
RE: Craps Game #14
Here's a mini test program demonstrating what I was talking about:
Code:
#include <stdio.h> #include <string.h> int str_toint(char *s) { int result = 0; int pow10 = 1; size_t i = strlen(s); do { result += pow10 * (s[--i] - '0'); pow10 *= 10; } while (i > 0); return result; } int main(void) { char *s = "234567"; int result = str_toint(s); printf("%d\n", result); return 0; }

Now you have an integer of the value: 234567.

NOTE: If you're okay with the way it is, then fine. But I could also include a data overflow check and change the return type to a boolean for success/failure in that case, and have an out param for the resulting integer to be used if the function returns true. What I mean by this, is *obviously* a string like "9999999999999999999999" won't fit into a 32 bit integer datatype.

Although, there's even more ways aside from atoi(), and even more if this was C++ and not C.
ArkPhaze
"Object oriented way to get rich? Inheritance"
Getting Started: C/C++ | Common Mistakes
[ Assembly / C++ / .NET / Haskell / J Programmer ]

Reply





Messages In This Thread
Craps Game - by chmod - 11-02-2013, 06:15 PM



Users browsing this thread: 1 Guest(s)