I use Borland C++ Builder and it has the itoa and atoi functions also. I have used them quite a bit - the problem using atoi is that it requires a char *, not a single char. Your sample code is taking newscore which is a single character; since it is missing the string terminator, it is then using the rest of newscore until it reaches the string terminator as the string to convert to integer. You can use atoi - you just need to convert each character or group that you want to use to a terminated string first.
I had the same problem in my code, which had to verify time entries stored in a character array as being valid, so I took both 1, 2 and 3 digits at a time from my character string and compared these parts to integers.
Good luck.
Ricky B
Edited by - RickyB on June 19, 2000 2:20:02 PM
Splitting integers into digits
Here is some code that uses sprintf() and works.. the sum should be 15 and that''s what it is...
Hope this helps.. there are numerous ways to do what you want... so you gotta pick one of the many ;P
int iNum = 12345; char sNum[10]; int sum = 0; sprintf(sNum, "%d", iNum); for (int i = 0; i < strlen(sNum); i++) sum += (sNum-''0''); sprintf(sNum, "%d", sum); MessageBox(sNum, "Test", MB_OK);
Hope this helps.. there are numerous ways to do what you want... so you gotta pick one of the many ;P
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement