Need help with displaying a result

This is a discussion on Need help with displaying a result within the C++ Programming forums, part of the General Programming Boards category; Hello again everyone, it's been awhile since I was here. I have another problem for anyone who is up for ...

Need help with displaying a result

Hello again everyone, it's been awhile since I was here. I have another problem for anyone who is up for the challenge. I have a code here that I will post in a second, and what it is is a code that someone inputs 2 strings of numbers. The code then turns the strings of numbers into 2 integer arrays. It then adds the 2 arrays piece by piece and inputs that data into a new array. I am having problems with carrying a 1 to a new decimal place beyond the highest place inputed. Example being inputs are 900 and 100, it will display 000 and give me an error. Does anyone know how I could put this last decimal place in? I have tried for about 5 hours now and am finally turning to you all. Thanks again!

I think you should actually start at the other end of the array. Think of it like this:

Code:

101
+ 109
---
110

You start on the right hand side of the sum. It's more logical and less complicated than the other method. While you're doing the same thing, it's nicer to read with a:

Code:

for (int i = MAXZIZE-1; i>0; i--)

In my opinion.

Anyways. Create a temp variable which is the sum of two current elements. Add them together. Add the remainder (%), of it to the sum element, and the most significant number to the element one place higher than the current bit.

I am reading over your code and I don't think I quite understand it. I have all the addition down. If you put in any number that doesn't need an additional space on the end, it adds them up pretty well. It's just any number that adds another decimal place that cuases an error. Sorry about the delay on the response, was AFK getting some supper.

One other error I just noticed. If I add numbers such as 25 and 250, i will get 500 instead of 275. I tried to correct this error earlier as well but failed. I set the initial values added to start at size3, which i made size3 the larger of the two sizes, and it gave me a jumble of errors. I am not sure how to fix this error either. I worked on that problem for many hours as well, but as I said, I still have no idea on where to go with it.

Edit: I would have put this in the statement above but i accidently did quick reply, sorry.

As Towmers suggested, start doing the sum at the end of the array and moving backwards to the front one element at a time. You then need to determine if the sum of those elements carry some value over to the next element. How do you do it on paper?

27
+
36

The sum of 7 and 6 is 13. You write 3 down and carry 1 over to the next sum adding it and repeating the process. 1 + 2 + 3 = 6. You write 6 down. The result is 63.

How to code if a number carries over or not? There's probably more than one way. One of them is to divide the result of each sum by 10 and take the integer portion.

Well I thank you both and anyone else who wants to share their ideas with me. I will try to figure this out using the ideas you both presented and anyone else presents. I am only in an intro to C++ class and my professor wants us to do this. Honestly, I feel this is sorta hard for a person in an intro to C++ class, but it is his class and I suppose he wants us to think about it, but I have tried for many hours. I will continue to try to figure this out, thanks again!

To anyone interested

Good news! I took a break, went back to my code, did some cout stuff in certain places to see what was going on and I figured it out! I can sometimes amaze myself! I would LOVE if someone would copy this code and throw gernades and nukes at it to see if there are any faults in it. Please, please do so, and if you find any let me know!

Edit: A small edit, I do believe the error I was creating was putting the numbers at the end of the array from right to left in decreasing 10ths places, instead of increasing 10ths places. Therefore it would count 450 as 054 and say 50 as 05 so when 450 +50 was inputed, it would produce 054+005=059 or as i had it output, 900. I fixed that small problem and it works like a charm now. Go ahead and put in 100 decimal digits and see if you can get the program to create an error. (if you put in 100 you can't let the 100th 10th's place total more than 10)