| INeedAHero (31) | |||||
|
Operator overloading seems to keep tripping me up. Here's my task (this is the same task as in my other topic, but since I'm asking about a different area, I felt it'd be best to make a new topic): 1.) The user types a gigantic, 40-digit number 2.) I cin it as a string (since int can't be that big) 3.) I turn that string into a character array 4.) I turn that character array into an int array 5.) I perform multiplication and/or addition on the array I essentially have everything working - the addition and multiplication algorithms work, the conversion algorithms work, everything is good. The issue is just overloading the cin ">>" operator. When I try to compile, I get this strange error: HugeInteger.cpp:24:73: error: âstd::istream& HugeInteger::operator>>(std::istream&, const HugeInteger&)â must take exactly one argument I call it strange, because when I make it one argument, it says I must take exactly two arguments! I'm at a bit of a loss here. Anyone see what's wrong? Here is my code:
And here's my .h
| |||||
|
|
|||||
| Peter87 (3908) | |
Remove HugeInteger:: and const from line 19 in the source file.
| |
|
|
|
| ne555 (4382) | |||
¿what does num1.copy(cArray, num1.size(), 0); do?By the way, your operators have weird behaviour.
| |||
|
|
|||
| Peter87 (3908) | |
This linein.arr[i] = in.atoi(in.arr[i]);you probably meant to write in.arr[i] = in.atoi(cArray[i]);.You don't really need cArray because you can use num1 directly. in.arr[i] = in.atoi(num1[i]);After this change you will notice that the digits will be written to the left most positions so you end up with very large numbers with many zeroes at the end but you can probably work it out. operator>> should also return is. | |
|
|
|
| INeedAHero (31) | |
|
Haha, I actually deleted my post because I fixed everything up to the point where you said I "can probably work it out". I'm trying to work out now. Hopefully I'll get it, if not I'll post again. Thanks for the help! | |
|
|
|
| INeedAHero (31) | |||
|
Well, this is strange. Here's the change I made to my function:
My logic was to alter the "for loop" so that the input would start from the end, a method I have used before with success. Yet my output is once again nonsense! I've tried playing around, but I can't see what's wrong here. It seems to me that my problem should be fixed.... Here's the ouput: enter number plox: 43 num1 = 43 size of integer is 2 enter number plox: 5 num1 = 5 size of integer is 1 num1 = -480 num2 = -1 object: -480 arr: -1433646464 The answer is: -8-1433646464 | |||
|
|
|||
| Peter87 (3908) | |
| If you do like that i will be out of bounds in num1. | |
|
|
|
| INeedAHero (31) | |
|
But....how? 'i' starts at 39, goes through the loop, decreases by 1, and then repeats until it's no longer greater than '39 - (number of digits in integer)' At what point does num1[i] go out of bounds?? | |
|
|
|
| Peter87 (3908) | |
| num1[i] is out of bounds if i >= num1.size(). | |
|
|
|
| INeedAHero (31) | |
| But if I want the 40th entry of the array to equal the last digit of the integer, and the integer is only, let's say, 5 digits long, wouldn't 'i' have to equal 39, which is greater than 5? | |
|
|
|
| Peter87 (3908) | |
| Yes, that's the problem. You should not use the same index to both in.arr and num1. | |
|
|
|
| INeedAHero (31) | |||||
|
Ah! Now I see. Thank you! But - this is so frustrating!!!!!!!! - when I run the function it gives me the wrong answer! This is how I corrected the problem:
This is my Main function:
This is the output: enter number plox 4 num1 = 4 size of integer is 1 BOUND = 38 . enter number plox 6 num1 = 6 size of integer is 1 BOUND = 38 . num1 = 4 num2 = 6 object: 4 arr: 6 The answer is: 64 Again, I don't see how this is happening?? | |||||
|
|
|||||
| Peter87 (3908) | |
|
You forgot to initialize carry in add. You need to define m outside the loop, otherwise it will not work if you have more than one digit because m is always 0 when you use it. | |
|
Last edited on
|
|
| INeedAHero (31) | |
|
Thanks a lot! I initialized add to zero, I defined m outside the loop (makes sense...I can't believe I didn't see that), and, believe it or not, it STILL isn't adding the numbers correctly!!! Is it possible that I need to initialize the cArray? I tried making it: char cArray[in.MAX] = {' '}; But, although that compiled, I still got nonsense answers. This is the output: 4 num1 = 4 size of integer is 1 BOUND = 38 . . enter number plox 5 num1 = 5 size of integer is 1 BOUND = 38 . . num1 = -484 num2 = -485 object: -484 arr: -485 The answer is: -969 | |
|
|
|
| INeedAHero (31) | |||
|
Okay, I found out that I had " >= bound" when it should be " > bound" I fixed that error, but of course it still doesn't add properly (though now it's in the same ballpark!) Here's my whole function:
And here's some output: 78 num1 = 78 size of integer is 2 BOUND = 37 . 8 . enter number plox 22 num1 = 22 size of integer is 2 BOUND = 37 . 2 . num1 = 87 num2 = 22 object: 87 arr: 22 The answer is: 109 | |||
|
|
|||
| INeedAHero (31) | |
|
GOT IT! Haha, m had to be set to the size of the integer, not zero. That's why it was switching them around. Actually, set to the size of the integer minus one since arrays are zero-based. Finally got this little troublemaker. Thank you so much to everyone who helped me, now I have an addition function that is actually working! Thank you so much! | |
|
|
|