Output Function Help

I have a constructor that assigns a single digit to each element of an array. For example if the user input (128) it would assign 1 to element 0, 2 to element 1, etc. I have a max size of 500 elements for this array. So there would be 497 0's after the 128.. How would I only output 128 and not the other 497 0's? Here's my code so far..

1
2
3
4
5
6
7
8
9
 bigint::bigint(int digit){

	int numdigits = log10((double)digit) + 1;

	for (int i=numdigits-1; i >= 0; i--)
	{
		value[i] = digit%10;
		digit /= 10;
	}


1
2
3
4
5
void bigint::output(){
                            
	for(int i=0; i <= MAXINT; i++)
		std::cout << value [i];
}
I think you go at it the wrong way. Say you want to display 100. How do you know to display two 0, and not the rest? I would suggest that you store the information in reverse order. If you assign 8 to element 0, 2 to element 1, and 1 to element 2, you would not get this problem.
For the other part, if you use the above mentioned convention, you can do something like

1
2
3
4
5
6
7
8
9
10
#include <iostream>
using namespace std;
int main()
{
   int a[5],i=0;
   a[0]=1;a[1]=2;a[2]=3;a[3]=0;a[4]=0;
   while(a[4-i]==0) i++;
   for(;i<=4;i++)
      cout<<a[4-i];
}
I still don't see how this would solve the problem. Okay so if the user did want to display 104 for example. And the max size they can input is 500 digits. Even if I assign 4 to element 0, 0 to element 1, and 1 to element 2, how would the program know to stop outputting the other 497 0's?
Look at the above code: on line 7 I skip all zeroes, starting from the end, until I get a non-zero integer, then output elements a[2],a[1],a[0], so I stored number 321. If I have number 104, I store a[0]=4,a[1]=0,a[2]=1, a[3]=...=a[499]=0. Now display the array backwards, skipping all zeroes at the end. If you feel uncomfortable storing the number backwards, store a[0]=...=a[496]=0, a[497]=1,a[498]=0,a[499]=4. Then increase i from 0, until a[i]==0. Then print a[i] from that i until 499
@ats15 Thank you for your help I figured it out now!
Topic archived. No new replies allowed.