Sep 16, 2013 at 2:48am UTC
Well I'm kind of confused because in the directions for the project it says we have to use a "character array". Is that different than a string?
Sep 16, 2013 at 2:52am UTC
A string of chars, an array of chars. Potato, potato... Err... You know what I mean.
Sep 16, 2013 at 2:59am UTC
Nevermind I'm an idiot I forgot I had it as a const in the header..
Sep 16, 2013 at 4:15am UTC
I would use char *number
and not char number[] in your function
so it should be something maybe like this
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>
const int MAX_SIZE = 3;
class BigInt
{
public :
BigInt( const char *value );
friend std::ostream &operator <<( std::ostream &stm , const BigInt &bi );
private :
int value[ MAX_SIZE ];
};
BigInt::BigInt( const char *value )
{
for ( int i = 0; i < MAX_SIZE; ++i )
{
this ->value[ i ] = -'0' + *value++;
this ->value[ i ] = *value - '0' ;
++value;
}
}
std::ostream &operator <<( std::ostream &stm , const BigInt &bi )
{
for ( int i = 0; i < MAX_SIZE; ++i )
{
stm << bi.value[ i ];
}
return ( stm );
}
int main()
{
//char array[] = "123";
//BigInt bi1( array );
BigInt bi1( "123" );
std::cout << bi1 << std::endl;
}
123
http://ideone.com/3KVcaw
*forgot link
Last edited on Sep 16, 2013 at 4:17am UTC
Sep 16, 2013 at 4:23am UTC
There's not much difference in this case whether you use a pointer to an array or use a pointer to an array (besides const-ness).
I would still pass in the size of the array as a second argument (or use template magic), so I can iterate according to the size of the array pointed to by the parameter.
1 2
for (size_t I(0); value[I] != '\0' && I < MAX_SIZE; ++I)
arr1[I] = value[I] - '0' ;
I would also do some checking through the array in case the user tries entering "123.jhgw87\/246~-93||".
Edit:
Oh, but what I would prefer most is using std::string. :)
Last edited on Sep 16, 2013 at 4:30am UTC