Strange extra zeros

I'm writing a program to reverse an integer. It works perfectly except that it has strange zeros. For example, if I input 123, the output is 03210 instead of 321. I can't find the error. Here's my code:
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
#include <iostream>
using namespace std;
const int ARRAY_SIZE=100000000;
int integer [ARRAY_SIZE];
int number;
int reverseDigits (int integer []);
int main ()
{
    char choice;
    int reverseInt;
    cout << "Reverse Generator" << endl << "++++++++++++++" << endl;
    do 
    {
        cout << "Enter an integer: " << endl;
        cin >> number;
        reverseInt= reverseDigits (integer);
        cout<<reverseInt;
        cout << endl <<"Do you want to repeat? ";
        cin >> choice;
    } while (choice=='Y' || choice=='y');
    return 0;
}
int reverseDigits (int integer[])
{
    int A, count, i=0;
    for (count=10; A>9; count*=10) 
    {
        A=number%count;
    }
    while (count>0) 
    {
        integer[i]=number/count;
        number=number%count;
        count/=10;
        i++;
    }
    while (i>=0) 
    {
        cout << integer [i];
        i--;
    }
}
That is a most interesting way to reverse a number.
Anyways, let me point out your mistakes.

First of all...
3
4
const int ARRAY_SIZE=100000000;
int integer [ARRAY_SIZE];

That is a MASSIVE array you're declaring there.
int can't even store close to 100 million digits, so why declare an array that big? (10 digits is sufficient)

25
26
int A, count, i=0;
for (count=10; A>9; count*=10)

A is being used uninitialized here.
Give it some initial value greater than 9.

35
36
37
38
39
while (i>=0) 
{
    cout << integer [i];
    i--;
}

When you first enter this while loop, i is 1 greater than it should be. (because you incremented i right before you exited the loop above this)

That's where the first 0 came from. (you're lucky it was 0 and not something like 4214816, or else you would've gotten the output 42148163210)

The last 0 comes from the fact that you're displaying the reversed number in your reverseDigits function, and then back in main(), you're displaying the value that reverseDigits returned. (which just happened to be 0, because you didn't write any return statement at the end of your reverseDigits function).

Hope this makes sense....
Last edited on
in my compiler there would be an error (vs 08) because uninitialized variable
1
2
3
for (count=10; A>9; count*=10)  {
        A=number%count;
}

but probably in your compiler 'A' have an garbage value (a very large amount of number) so your 'for' is done and the 'count' is well calculated...

CMIIW
This looked interesting so I decided to give this a go:

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
/*

Reverses a number

*/

#include<iostream>
using namespace std;

int main(){

	int k, r;

	cout<<"Enter a number: ";
	cin >> k;

	for(int i=10; k>0; i *= 10){	r = k%i;
					cout << r * 10/i;
					k -= r;	}

	cout << '\n';

return 0;

}


Testing it out gives:

Enter a number: 46321234
43212364
Thank you so much! I've got it now.
Topic archived. No new replies allowed.