Program that prints out all digits of an integer not working

Pages: 123
Title. Even more bizzarely, it prints something different every time it's run.

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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int explode(int number, int array[])
{
    int count = 0;
    int thing = 0;
 int digit = 0;
 int returner = 0;
 for(int i = 0; i < number; i++) {
          digit = number % 10;
          number = number / 10;
          
         thing += digit;
         returner = number % 10;
          }
          cout << digit;
return returner;
}

int main()
{
	int digits[10];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
	numdigits = explode(n,digits);
	
	cout << "[" << digits[0];
	for( int i = 1; i < numdigits; i++ )
		cout << "," << digits[i];
	cout << "]" << endl;
}
Well, you never store any values in your array, so it just has the same random garbage values from when the program started.
OK, now I have:
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int explode(int number, int array[])
{
    int count = 0;
    int thing = 0;
 int digit = 0;
 int returner = 0;
 int i = 0;
  while(number > 0) {
          int digit;
          digit = number % 10;
          array[number] = digit;
          
          number = number / 10;
          i++;
          }
          cout << array[number];
return i;
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
	numdigits = explode(n,digits);
	
	cout << "[" << digits[0];
	for( int i = 1; i < numdigits; i++ )
		cout << "," << digits[i];
	cout << "]" << endl;
}

but still nothing.
I now have
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void explode(int number,int array[])
{
    
    while (number > 0) {
        int digit = number % 10;
        cout << digit << '\n';
        number /= 10;
    }
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
//	numdigits = explode(n,digits);
	
	cout << "[";
	while (n > 0) {
        int digit = n % 10;
        n /= 10;
        digits[digit] = digit;
       // cout << digits[digit];
        
    }
	cout << "]" << endl;
}

It works (if you uncomment the commented cout) but it prints in reverse. How do I fix this?
You have the car set to reverse and you're asking my why it's not in drive - I'm not sure what to tell you.

Don't loop in reverse? Line 28 should be a for loop.
What should the condition be, and is the only thing needed to make it print right the for loop?
Why not just use a traditional for-loop?

for(int i = 0; i < n; ++i)
Doesn'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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

void explode(int number,int array[])
{
    
    while (number > 0) {
        int digit = number % 10;
        cout << digit << '\n';
        number /= 10;
    }
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
//	numdigits = explode(n,digits);
	
	cout << "[";
	for(int i = -1; i < n; i++) {
        int digit = n % 10;
        n /= 10;
        digits[digit] = digit;
        cout << digits[digit];
        
    }
	cout << "]" << endl;
}
Are you pranking me? Why did you start your loop at -1 instead of 0?

Lines 29 to 31 should not exist - that should have already been don by your function.
Line 32 needs to use i
I fixed Line 32, but it still doesn't work...
Please post your current code so I can see exactly what you mean by the word "fixed"
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int explode(int number,int array[])
{
    int digit = number % 10;
    while (number > 0) {
        int digit = number % 10;
      //  cout << digit << '\n';
        number /= 10;
    }
    return digit;
    
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
	numdigits = explode(n,digits);
//	cout << numdigits << endl;
	int array[numdigits];
	cout << "[";
	for(int i = 0; i < n; i++) {
        int digit = n % 10;
      //  n /= 10;
      //  digits[digit] = digit;
        cout << array[numdigits];
        
    }
	cout << "]" << endl;
}

not working
Line 29 should not exist - you want to use your existing digits array.

On line 35 you want to use digits[i]
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int explode(int number,int array[])
{
    int digit = number % 10;
    while (number > 0) {
        int digit = number % 10;
      //  cout << digit << '\n';
        number /= 10;
    }
    return digit;
    
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
	numdigits = explode(n,digits);
//	cout << numdigits << endl;
	int array[numdigits];
	cout << "[";
	for(int i = 0; i < n; i++) {
        int digit = n % 10;
      //  n /= 10;
      //  digits[digit] = digit;
        cout << array[i];
        
    }
	cout << "]" << endl;
}

Nope, still not working....
I think you saw my deleted post - read my edit post:
http://www.cplusplus.com/forum/beginner/157073/#msg805955
Not working
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
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;

int explode(int number,int array[])
{
    int digit = number % 10;
    while (number > 0) {
        int digit = number % 10;
      //  cout << digit << '\n';
        number /= 10;
    }
    return digit;
    
}

int main()
{
	int digits[100];
	int numdigits;
	
	int n;
	cout << "Enter number: ";
	cin >> n;
	
	numdigits = explode(n,digits);
//	cout << numdigits << endl;
//	int array[numdigits];
	cout << "[";
	for(int i = 0; i < n; i++) {
        int digit = n % 10;
      //  n /= 10;
      //  digits[digit] = digit;
        cout << digits[i];
        
    }
	cout << "]" << endl;
}
Your explode function still isn't changing the values in the array parameter.
What do I need to change to make it do that?
Well, after deleting line 8, you'll need a variable to keep track of how far along into the array you are (I would call it i) and then inside the while loop you would store a value at that point in the array and then increment that variable.
What value would I store at array[i] though? It still prints a load of garbage with array[i] = digit.
Pages: 123