Output Problem

I have a program with an array of 500 elements. The user can input either an int or char to initialize it a value. So if the user inputs bigint bi(1642) it will assign 1 to element 0, 6 to element 1, etc. Keep in mind there are now 496 other elements that are unassigned. When I want to output this how do I tell it to stop outputting once it hits the last assigned element?

Obviously this doesn't work because it is outputting from 0-MAXINT but im not sure what else to do.

1
2
3
4
5
6
7
void bigint::output(){

	for(int i=0; i < MAXINT; i++)
	{
			std::cout << value[i];
	}
}
Last edited on
Or am I just doing this the completely wrong way? Can anyone point me in the right direction?
Can users pick any value? i.e. 0 or negative value as well?
They can pick 0 but not negative numbers.
So they can pick 0 and any positive numbers
closed account (D80DSL3A)
You had the magic number in a local variable 'numdigits' in the constructor (from one of your many other threads on this problem). Make that a member variable and use it in your output().

Please quit creating threads on this same subject (3 active on the 1st page alone). You're just alienating everyone by doing that. Follow the development through in a single thread and bump it when necessary.

Don't forget to reply in some manner to all who contribute.
Okay thank you, sorry I am pretty new to the site so I wasn't sure about that. So if I make 'numdigits' a member variable that will always store how many numbers are active in the array and I can use that instead of MAXINT? So essentially something like this:

1
2
3
4
5
6
7
void bigint::output(){

	for(int i=0; i < numdigits; i++)
	{
			std::cout << value[i];
	}
}
closed account (D80DSL3A)
Yes, that should work.

Policy re. posting practices should be posted clearly on this site (and it isn't), so it's our fault too.

Will you be required only to display these bigints, or will you be adding them (overloading operator + perhaps)? If so then storing them as most significant digit 1st will cause quite a headache later, though it is most convenient right now for the purpose of displaying them.
Last edited on
We will be adding, subtracting, multiplying all next so what is my other option then if this will cause a headache later?
closed account (D80DSL3A)
If you store them least significant digit 1st then the ones digits, tens digits, etc. will be aligned in the same element of the array. This makes addition simple:
1
2
3
sum = value[i] + x.value[i] + carry;
result.value[i] = sum%10;
carry = sum/10;


Your constructors would have to be rewritten though.
So would I have to store it in reverse order? So if the user input 4912 then 2 would be element 0, 1 would be element 2, etc?
closed account (D80DSL3A)
Yes. The process is fairly easy. You get the least significant digit by using the % operator. If iVal = 4912 then:
4912%10 = 2 = value[0]
4912/10 = 491
491%10 = 1 = value[1]
491/10 = 49
49%10 = 9 = value[2]

and so on until iVal == 0. Increment numdigits as you go.

For the char array version:
1
2
3
numdigits = std::strlen( str );
for( int i=0; i<numdigits; ++i )
    value[i] = (int)( str[(numdigits-1)-i] - '0' );
Last edited on
@fun2code Thank you so much for all of your help, I figured it all out now!
Topic archived. No new replies allowed.