Separating Integer into Digits

This is far as I gotten, I have been able to separate the digits;however, they are displayed backwards. Any help would be nice.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>
using namespace	std;
int main()
{
int x;
int digit;
   cout << "input an integer: ";
   cin >> x;

   do 
{
   digit=x % 10;
        x= x / 10;
   cout	<< digit <<" "; 
}  while(x > 0);

return 0;

}

Last edited on
Try putting one iteration of the while in a function. And while x>0, call the function again in the function. Now print the number only after each iteration has called the next iteration. The less significant numbers will only be printed after the more significant ones.
It might not be clear, try it on paper to understand

1
2
3
4
5
6
print_digit(int x){
 if(x>0){
  print_digit(x/10);
 }
 cout << x%10 << " ";
}



closed account (D80DSL3A)
@bartoli That almost works.
Aside from the trivial compile error (no return type given-surely an oversight) it prints a leading 0.
Suggested fix:
1
2
3
4
5
6
void print_digit(int x){
 if(x>9){
  print_digit(x/10);
 }
 cout << x%10 << " ";
}

That's a nice clean recursive solution though.

EDIT: Here's an (overcomplicated?) iterative solution:
1
2
3
4
5
6
7
8
9
10
11
12
void print_digit(int x)
{
	int tenPow = 1;
	while( x/tenPow >= 10)// buildup tenPow until > x/10
		tenPow *= 10;
	do
	{
		cout << x/tenPow <<" ";
		x = x % tenPow;
		tenPow /= 10;		 
	}while(tenPow > 0);
}
Last edited on
Ive never seen a function call within its same function. very creative solution, and thank you for your help. sadly I still don't see how it works properly.
Last edited on
Not my idea, i saw it in general discussions one day, but couldn't find back the post. You also have the solution of using itoa() to convert your number in a string, then loop backward inside the string and print chars one by one. This has the advantage to work on negative numbers too
smkelsey wrote:
Ive never seen a function call within its same function.

it's called recursion and has the following features:
1. in one case, it will call itself with a new argument (the recursion: Line 3)
2. otherwise, it will return a result or do something besides calling itself

in the stages where it's calling itself, the effect is same as pushing results onto a stack (1)
when it hits the end case (2), the stack unwinds

think about what happens when you call a function foo() - it acts the same way - the computer will call all the functions inside foo until it hits the end-brace; when that happens, you will pop back out of the function - it's the same with recursion

because a stack acts as LIFO, last-in-first-out, its net effect is the same as "reversing" your initial results

so with your initial code, had you pushed the digits onto a stack (instead of printing them out immediately) and then unwound the stack afterwards while printing, you would get the same result
thanks kfmfe04, your post was very informative. How would i go about doing the same thing, but instead using letters rather than numbers. seeing is how we are using %10 get the last digit i cant imagine how i would go about analyzing letters. -ex (HELLO)
You mean from a string? You can address single characters of a string with the index operator, for example
1
2
3
std::string hello("hello world");
std::cout<<hello[0]; 
std::cout<<hello[10];


output:

hd
Thanks, I thought that was how it was, but i wasn't entirely sure.
Topic archived. No new replies allowed.