Splitting integers into digits
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
jumble-----------
Check out the function itoa() (Integer TO Array). If you want to reverse the effect, there's atoi() (Array TO Integer).
/. Muzzafarath
Mad House Software
Edited by - Muzzafarath on June 19, 2000 10:59:40 AM
#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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
June 19, 2000 10:07 AM
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.
June 19, 2000 10:13 AM
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;
}
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;
}
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
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
June 19, 2000 11:54 AM
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:
and it spurted back at me:
which isn''t what i want. BTW i am using MSVC++.
it's hard to remember scripture if you're an atheist
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:
9876876766
which isn''t what i want. BTW i am using MSVC++.
it's hard to remember scripture if you're an atheist
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
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
jumble-----------
int x=0
int number = 8562
do {
mydigit[x] = number/(10^x);
x++;
} while (x<10000000);
or something
int number = 8562
do {
mydigit[x] = number/(10^x);
x++;
} while (x<10000000);
or something
You can use:
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
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
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
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
jumble-----------
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement