#### Archived

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

# Splitting integers into digits

This topic is 6672 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## 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 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 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

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

##### Share on other sites
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 on other sites
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 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

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

##### Share on other sites
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 ));}

and it spurted back at me:

9876876766

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

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

##### 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 on other sites
int x=0
int number = 8562

do {
mydigit[x] = number/(10^x);
x++;
} while (x<10000000);

or something

##### 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 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

1. 1
2. 2
3. 3
Rutin
16
4. 4
5. 5

• 10
• 11
• 14
• 10
• 25
• ### Forum Statistics

• Total Topics
632649
• Total Posts
3007644
• ### Who's Online (See full list)

There are no registered users currently online

×