### how to split an integer into digits?

Hello,

I want to split an integer into digits. could you please show me how in a very simple way?

I'll use that way to convert binary to decimal.

thank you
You can stream the integer to a stringstream, so you can parse individual digits and use the length of the string to determine what power of 2 to multiple by.
Hi there,

could you please give me example?

I can't understand how to split integer from this link:

http://www.cplusplus.com/reference/sstream/stringstream/

thank you
 ``123456`` ``````int bin(001110); std::stringstream ss; ss << bin; //Stores 001110 as individual digits in a buffer (no longer an integer) //str() is a function that returns a std::string object for(size_t I(0); I < ss.str().size(); ++I) std::cout << ss.str()[I] << '\t'; //Because it is a string, you can use [] to access individual characters, or "digits" ``````

I've heard of a new "to_string()" function from C++11 that let's you convert a number to a std::string directly.

If you wanted to, you could also do this by hand. (The way I would have to do it if this were a school assignment) like:

 ``1234567891011121314151617181920212223242526`` `````` cout << "Enter an integer " << endl; int number; cin >> number; //convert the number into digits //lets say the number is 12345, of course this would require 5 variables, int n1,n2,n3,n4,n5; n1 = number%10; number /= 10; n2 = number%10; number /= 10; n3 = number%10; number /= 10; n4 = number%10; number /=10; n5 = number%10; number /= 10; cout << n1 << endl; cout << n2 << endl; cout << n3 << endl; cout << n4 << endl; cout << n5 << endl; ``````
@erock

or you could just do this..
 ``123456789`` ``````#include int main() { std::string number; std::cout << "Enter an integer " << std::endl; std::getline( std::cin , number ); for( const auto &it : number ) std::cout << it /*-'0' if you want them to be ints and not chars*/ << std::endl; }``````

I would still just use string streams. << >>
Last edited on
Actually, erock does have a good idea on the approach. Something like:
 ``12345678`` ``````int bin(0011101); int convert(0); int counter(0); while(bin > 0){ convert += (bin%10) * pow(2, counter); bin /= 10; ++counter; }``````

is a lot easier than my approach of converting the integer into digits first.
You can also use counter <<= 1 instead of pow to square I believe. AFAIK pow is best used for doubles and not ints.

I meant ( 1 << counter )
Last edited on
@giblit

I am new to programming as well so I have not yet seen anything like that. Thanks though! I have a hard time turning off the Uni way of doing everything from scratch.
`1 << counter` might be faster, but empror9 might not know how to use bitwise operators yet.
Everyone here is missing the point. This is an elementary homework problem.

Given an integer, say `int n = 4567;`, the way to split off individual digits is by using the integer division and remainder operators.

 ``12`` ``````int ones_digit = n % 10; // remainder of division by ten is one's place int n = n / 10; // divide by ten, forget old one's place digit ``````

Hope this helps.

BTW, that's a prime number there. :O)
Last edited on
But he wants to convert binary to decimal.
OP wrote:

I'll use that way to convert binary to decimal.

I would like to point out that erock's and my (revised) approaches are similar to what you put down.
Sorry, I'm tired and missing stuff. I really just saw a bunch of stuff about stringstream and string and the like and stopped reading.

Sorry. <:-|
Hi guys, I'm sorry about my late response. I have read all your replies here and I found erock 88 code very simple, but It's only print the rightmost digit so I add some string to see which `n` is the output. I get only rightmost digit without any string. here is the code

 ``123456789101112131415161718192021222324252627282930313233`` ``````#include using namespace std; int main() { cout << "Enter an integer " << endl; int number; cin >> number; //convert the number into digits //lets say the number is 12345, of course this would require 5 variables, int n1,n2,n3,n4,n5; n1 = number%10; number /= 10; n2 = number%10; number /= 10; n3 = number%10; number /= 10; n4 = number%10; number /=10; n5 = number%10; number /= 10; cout << "n1 " << n1 << endl; cout << "n2 " <

thank you again
The program works fine. the difference between erock's code and mine is that his only works with numbers exactly 5 digits long and adjusting it for different sized numbers would be more cumbersome than needed. Mine is written to work with integers of any length, but does not split up the number into digits as it converts a binary number to integer directly. It can be adapted for converting to digits, however.

If you don't want the result to be displayed in reverse, then reverse the order you display the n# variables.
I'm sorry it works now, I just had some errors on my machine so I restart it and now works fine.

thank you
Topic archived. No new replies allowed.