Program that prints out all digits of an integer not working

Pages: 123
Please post your current code.
I got it working with recursion..... but it prints one too many comma, and I don't know how to fix it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int explode(int number,int array[])
{
    int functi = 0;
    int digit = number % 10;
    while (number > 0) {
     //   int digit = number % 10;
        array[functi] = digit;
        functi++;
      //  cout << digit << '\n';
        number /= 10;
    }
    return digit;
    
}

void explode(int number)
{
     int ctrack = 0;
    if (number <= 0) return;
    int digit = number % 10;
    explode(number/10);
    while(ctrack < 1) {
    cout << digit; 
    ctrack++;
    cout << ",";
}
}


int main()
{
    int numdigits[100];
    int n;
    cout << "Enter number: ";
    cin >> n;
  //  int digit = explode(n,numdigits[]);
    cout<<"[";
     explode(n);
    cout<<"]\n";
}
Don't print a comma when the number is less than 10?

Not sure why you decided to switch to recursion instead.
And how would I do that? What would I need to put in, and where?
If you decide to stick to using recursion, then you would need to surround line 29 with an if-statement.
What condition?
Didn't work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int explode(int number,int array[])
{
    int functi = 0;
    int digit = number % 10;
    while (number > 0) {
     //   int digit = number % 10;
        array[functi] = digit;
        functi++;
      //  cout << digit << '\n';
        number /= 10;
    }
    return digit;
    
}

void explode(int number)
{
     int ctrack = 0;
    if (number <= 0) return;
    int digit = number % 10;
    explode(number/10);
    while(number < 10) {
                 number++;
    //cout << digit; 
    ctrack++;
    if(number >= 10) {
    cout << ",";
}
}
//while(ctrack < 10) {
  //        cout << digit;
    //      ctrack++;
      //    }
}


int main()
{
    int numdigits[100];
    int n;
    cout << "Enter number: ";
    cin >> n;
  //  int digit = explode(n,numdigits[]);
    cout<<"[";
     explode(n);
    cout<<"]\n";
}
Fix the indentation of your code. You have some lines inside of some scopes that you do not want them in.

By the way: with a recursive solution, you should not need to use any looping structures at all.
What exactly do I need to change to fix this? You said I don't need to use any loops, so does that mean that I need to remove code (the loops) instead of add code?
Start by fixing the indentation and then rereading your code. I get the impression you don't even understand what you have written. I can't help you unless you know what you are doing.
Here:
1
2
3
4
5
6
7
8
9
10
11
12
13
void explode(int number)
{
    int ctrack = 0;
    if (number <= 0) return;
    int digit = number % 10;
    explode(number/10);
    cout << digit; 
    

    if(number >= 10) {
    cout << ",";
    }
    }

I don't understand what;s wrong, but it doesn't work.
http://ideone.com/H6h0d8

It almost works. The issue is that you recurse before outputting. Move line 6 to after line 12.

By the way, ctrack is not used, you probably want to remove it.
Except now it prints backwards. How do I fix that?
Oops, for some reason I thought it already printed backwards - you had it closer to correct before. My bad.

You just need to output after the comma:
http://ideone.com/7UPbLw
I realized I needed to use iteration and arrays (not recursion) to finish it, and now I'm having a weird problem; some numbers print fine, some print backwards, and some print wrong but with odd differences. Can someone figure out what is wrong?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
include <iostream>
using namespace std;
int explode(int number,int array[])
{
    int numberOfDigits = 0;

    while (number > 0) {
        number /= 10;
        numberOfDigits++;
    }

    return numberOfDigits;
}
int printDigits(int number,int array[])
{
    int digit = number % 10;
    array[digit] = digit;
    while (number > 0) {
        int digit = number % 10;
        int numbe = number;
         array[digit] = digit;
        cout << array[digit];
       
        number /= 10;
    }
    int i = 0;
    for(i = 0; i < number/2; i++) {
            array[i] = number-i-1;
            }
    return array[i];
            
}

int main ( ) {
    int number;
    int array[10];
    cout << "Enter number: ";
    cin >> number;
    cout << "[";
     printDigits(number,array);
     cout << ",";
     cout << "]";
}
array[digit] = digit;

This is wrong. You need to use proper indexes into the array.
What are the correct indexes?
Surely you know how to loop through arrays? Have you slept recently?
Bump
Pages: 123