Archived

This topic is now archived and is closed to further replies.

jumble

Splitting integers into digits

Recommended Posts

ARGGHHH! This ones driving me insane... i have an integer. Lets say it has the value of 5678. I need to "split" it up so i have 5 and 6 and 7 and 8 all in seperate variables / or an array. I''m pulling my hair out on this... i''ve tried converting the integer to a string then incrementing a pointer to it so i can get each value... but that kinda didn''t work and i dont know why... Any help appreciated. it's hard to remember scripture if you're an atheist

Share this post


Link to post
Share on other sites
Check out the function itoa() (Integer TO Array). If you want to reverse the effect, there's atoi() (Array TO Integer).

    
#include <stdio.h>

int main(void)
{
int test = 12345;
char array[50];

itoa(test, array, 10);
// The last argument (10) represents the base of the number system (in this case decimal, base 10)

// Then you can access the numbers like so:

if(array[0] == '1') printf("The first number is 1");

// Just remember that the ' ' signs around the numbers are important.


return 0;
}

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 19, 2000 10:59:40 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I''m pretty sure itoa(...) is a MSVC++ specific function. You could use sprintf(...) if you''re running a different C/C++ system. Then when you access an array element, just subtract the ASCII ''0'' (48 I think) if you want to get the actual number.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Forgot to post how to use sprintf(...) in case you don''t know.

int sprintf( char *buffer, const char *format [, argument] ... );

The first parameter is just a buffer, the second is the printf-esque format description.. for a single integer it would be "%d", and then you have the arguements themselves.

so a code snippet would look be like..
#include
#include

int main()
{
while(true)
{
int num;
char buffer[11];
//do stuff to give num a value
cin>>num;
sprintf(buffer, "%d", num);
int rep=0;
while(buffer[rep]!=0)
cout< cout< }
return 0;
}

Share this post


Link to post
Share on other sites
quote:
Original post by Anonymous Poster

I'm pretty sure itoa(...) is a MSVC++ specific function. You could use sprintf(...) if you're running a different C/C++ system. Then when you access an array element, just subtract the ASCII '0' (48 I think) if you want to get the actual number.


My code compiles with DJGPP, which means it's not MSVC++ specific. But itoa() isn't standard C though, but I'm pretty sure it will work with most compilers anyway.

/. Muzzafarath
Mad House Software

Edited by - Muzzafarath on June 19, 2000 11:21:46 AM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Yeah when i said "i''ve tried converting the integer to a string" i actually meant i had used the itoa() function. It didnt produce the results i wanted. I wrote this console program before i posted:

    
void main()
{
char newscore[6];
int score = 9876;

itoa(score, newscore, 10);

for (int i=0;i<4;i++) printf("%i\n", atoi( &newscore<i> ));
}


and it spurted back at me:


9876
876
76
6


which isn''t what i want. BTW i am using MSVC++.

it's hard to remember scripture if you're an atheist

Share this post


Link to post
Share on other sites
thats me above btw

Also in case you're wondering i need to compare the digits in integer form against some other integers, therefore the atoi function.

Thanks

Edited by - jumble on June 19, 2000 12:58:06 PM

Share this post


Link to post
Share on other sites
You can use:
    
int intoarray(unsigned source, int dest[])
{
int tmp=0;
{
dest[tmp]=source-(source/10)*10); //element is remainder of division

source/=10; //divide source by 10... drop the remainder

tmp++; //advance to the next array element

} while source>0; //the while is after so we will always pick up one digit.

return tmp;
}

if you call it like a=intoarray(1234, array);
a will = 4, the number of digits in the array. And, the array elements will be
array[0]=4, array[1]=3, array[2]=2, array[3]=1. Note that they this is 10^0*4 + 10^1*3 + 10^2*2 + 10^3*1.

This routine doesn't handle negative numbers. That would be an additional test that shouldn't be too hard to add (test at the beginning and resolve the test before the return).


Edited by - MartinJ on June 19, 2000 1:54:18 PM

Share this post


Link to post
Share on other sites
Hey, thanks everybody... i was just testing out the sprintf statement (turns out it generates the same thing as my atoi() routine above). I think i got this wrapped up now... cheers

oh yeah you could use the abs() function in math.h to find the absolute of a number, thereby getting past the negative number thang in your function Martin.

it's hard to remember scripture if you're an atheist

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Here is some code that uses sprintf() and works.. the sum should be 15 and that''s what it is...


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);
[/CODE]

Hope this helps.. there are numerous ways to do what you want... so you gotta pick one of the many ;P

Share this post


Link to post
Share on other sites