"COUT" doesn't work for "char *"!!!
Apr 15, 2013 at 10:17am UTC
HI!
I wrote this program, it is not complete now.
But let me know if you know!!! THNX!
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <cstring>
using std :: cout ;
using std :: endl ;
class BigInteger
{
public :
void AllocFill () ;
void Add () ;
private :
char * BI1 , * BI2 , * Result ;
} ;
void BigInteger :: AllocFill ()
{
short arrsize ;
//Here I allocate BI1 and fill it with random numbers
arrsize = rand () % 900 + 100 ;
BI1 = new char [arrsize] ;
cout << "arrsize = " << arrsize << endl << "BI1 = " ;
for (short i = 0 ; i < arrsize ; i ++)
BI1 [i] = rand () % 10 + '0' ;
cout << BI1 ;
cout << endl ;
//Here I allocate BI2 and fill it with random numbers
arrsize = rand () % 900 + 100 ;
BI2 = new char [arrsize] ;
cout << "arrsize = " << arrsize << endl << "BI2 = " ;
for (short i = 0 ; i < arrsize ; i ++)
BI2 [i] = rand () % 10 + '0' ;
cout << BI2 ;
cout << endl ;
}
void BigInteger :: Add ()
{
short REsize , inBI1 , inBI2 ;
REsize = strlen (BI1) > strlen (BI2) ? strlen (BI1) : strlen (BI2) ;
Result = new char [REsize + 1] ;
//Here I initialize the whole Result homes to zero, I think the problem is here
for (short i = 0 ; i < REsize + 1 ; i ++)
Result [i] = 0 ;
inBI1 = strlen (BI1) ;
inBI2 = strlen (BI2) ;
while (REsize > 0)
{
if (inBI1 > 0 && inBI2 > 0)
{
Result [REsize] += BI1 [inBI1] + BI2 [inBI2] - 48 ;
if (Result [REsize] > '9' )
{
Result [REsize] -= 10 ;
Result [REsize - 1] = 1 ;
}
}
if (inBI1 <= 0)
{
Result [REsize] += BI2 [inBI2] ;
}
if (inBI2 <= 0)
{
Result [REsize] += BI1 [inBI1] ;
}
inBI1 -- ;
inBI2 -- ;
REsize -- ;
}
//All things to here seems to work fine, my problem is that it doesn't cout the Result
cout << endl << endl << endl << "ASB" ;
cout << Result ;
}
int main ()
{
srand (time (NULL)) ;
BigInteger test ;
test.AllocFill () ;
test.Add () ;
}
Apr 15, 2013 at 10:24am UTC
What shall we know? And what does mean your stupid statement that ""COUT" doesn't work for "char *"!!!"? Does it mean that the compiler issues an error? When show the error message.
EDIT:
If you use a statement like this
cout << BI1 ;
where BI1 is a character array or a pointer to char then the array itself shall contain the terminating zero character. Your dynamically allocated array pointed by BI1 has no such a character.
You can change for example function AllocFill the following way
1 2 3 4 5 6
BI1 = new char [arrsize] ;
cout << "arrsize = " << arrsize << endl << "BI1 = " ;
for (short i = 0 ; i < arrsize - 1 ; i ++)
BI1 [i] = rand () % 10 + '0' ;
BI1[arraysize-1] = '\0' ;
cout << BI1 ;
Last edited on Apr 15, 2013 at 10:35am UTC
Apr 15, 2013 at 10:45am UTC
THank you!
SORRY for my bad English.
I did it but it still doesn't do what I want.
I changed the code a little bit, I wrote it with CodeBlocks 12.11.
If you have codeblocks, can you please test it!! thanks.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
#include <cstdlib>
#include <time.h>
#include <iostream>
#include <cstring>
using std :: cout ;
using std :: endl ;
class BigInteger
{
public :
void AllocFill () ;
void Add () ;
private :
char * BI1 , * BI2 , * Result ;
} ;
void BigInteger :: AllocFill ()
{
short arrsize ;
arrsize = rand () % 900 + 100 ;
BI1 = new char [arrsize] ;
cout << "arrsize = " << arrsize << endl << "BI1 = " ;
for (short i = 0 ; i < arrsize ; i ++)
BI1 [i] = rand () % 10 + '0' ;
cout << BI1 ;
cout << endl ;
arrsize = rand () % 900 + 100 ;
BI2 = new char [arrsize] ;
cout << "arrsize = " << arrsize << endl << "BI2 = " ;
for (short i = 0 ; i < arrsize ; i ++)
BI2 [i] = rand () % 10 + '0' ;
cout << BI2 ;
cout << endl ;
}
void BigInteger :: Add ()
{
cout << "\n\nBI1 = " << strlen (BI1) << "\n\nBI2 = " << strlen (BI2) << endl << endl ;
short REsize , inBI1 , inBI2 ;
REsize = strlen (BI1) > strlen (BI2) ? strlen (BI1) : strlen (BI2) ;
Result = new char [REsize + 1] ;
for (short i = 0 ; i < REsize ; i ++)
Result [i] = 0 ;
Result [REsize - 1] = '\0' ;
cout << Result ;
inBI1 = strlen (BI1) ;
inBI2 = strlen (BI2) ;
while (REsize > 0)
{
if (inBI1 > 0 && inBI2 > 0)
{
Result [REsize] += BI1 [inBI1] + BI2 [inBI2] - 48 ;
if (Result [REsize] > '9' )
{
Result [REsize] -= 10 ;
Result [REsize - 1] = 1 ;
}
}
if (inBI1 <= 0)
{
Result [REsize] += BI2 [inBI2] ;
}
if (inBI2 <= 0)
{
Result [REsize] += BI1 [inBI1] ;
}
inBI1 -- ;
inBI2 -- ;
REsize -- ;
}
cout << Result ;
}
int main ()
{
srand (time (NULL)) ;
BigInteger test ;
test.AllocFill () ;
test.Add () ;
}
Apr 15, 2013 at 10:45am UTC
you can use accessor to get the
1 2 3 4 5 6
char *
//example
char *getDate(){
return date;
}
Apr 15, 2013 at 10:46am UTC
I will not test your code. If you want that somebody will test your code then please pay the work.
And I do not see what you changed according to my comment.
Last edited on Apr 15, 2013 at 10:47am UTC
Apr 15, 2013 at 2:24pm UTC
@Ardeshir81 Perhaps it's because you are assigning
all of the elements to 0 at line 46:
1 2
for (short i = 0 ; i < REsize ; i ++)
Result [i] = 0 ;
This would cause cout to stop at the 1st character.
I think you want
Result [i] = '0' ;
instead.
Also line 59 should perhaps be:
Result [REsize - 1] = '1' ;
@vlad. He did add a line in accordance with your recommendation, though not in his AllocFill() function as you showed, and where he does also need it. See line 47.
Also, that would be another report for the bullying.
Topic archived. No new replies allowed.