ANY number into digit

Hello everyone,

I have a question. Could anyone write an algorythm for 'How to put any number into digits?'

Ex: If i type 12345, it should be 1 , 2 , 3, 4 ,5. This should fit with any number.
Thanks for help.
How about this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
void num2digits(int num, vector<int> &digits)
{
	digits.erase(digits.first(), digits.last()); //Empty the container
	
	//Find how many digits were entered
	int size = 0;
	for (int decimal = 1; num/decimal != 0; decimal*=10)
		size++;

	while (size-- > 0)	
		digits.push_back(num / pow((int)10, size))
	
	return digits
}


where int pow(int, int) is:

1
2
3
4
5
6
7
8
9
int pow(int x, int y)
{
	int output=1;

	for (int i=0; i<y; i++)
		output *= x;

	return output;
}
Last edited on
Here's one based on strings without any kind of input validation.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <algorithm>
#include <iterator>

using namespace std;

int main() {
    cout << "Enter your number: ";
    string n;
    getline( cin, n );
    if( n.size() ) {
        copy( n.begin(), n.end() - 1, ostream_iterator< char >( cout, ", " ) );
    }
    copy( n.end() - 1, n.end(), ostream_iterator< char >( cout, "\n" ) );
    return 0;
}
Enter your number: 12345
1, 2, 3, 4, 5
Last edited on
Guys, thanks for help, but my algorythm requires to put any number into digit, then find wich digit = 2, and then multiply those digits, that = 2.
At risk of asking a silly question, if you actually want " then find wich digit = 2, and then multiply those digits, that = 2.", why didn't you say so?
Because there can by a lot variations... I need to know how to put any number into digit. Then i could use that digit..
MooreCM's code puts any number into digit, and then you can use that digit. That's what you asked for.
Topic archived. No new replies allowed.