For this program I am supposed to add two numbers together, up to 20 digits, using arrays...i have most of it working but i cant figure out how to solve the carrying over problem when the two numbers added are greater than 9....here is what i have so far

#include <iostream>
#include <iomanip>

using namespace std;

int main()

        {
        int count = 0,  number;
        char first[20], second[20], sum[21];

        {
        cout << "Enter the augend: ";
        cin >> noskipws >> first[0];
        cout << endl;
        while( first[count] != '\n')
                {
                count++ ;
                cin >> first[count];
                }
        for ( number = count-1; number >= 0; number--)
                cout << endl;

        count=0;
        cout << "Enter the adden: ";
        cin >> noskipws >> second[0];
        cout << endl;
        while( second[count] != '\n')
                {
                count++;
                cin >> second[count];
                }
        for(number = count-1; number >= 0; number--)
                cout << endl;

        }
        number=0;
        for(number=0;number<count;number++)
        {
                sum[number] = (first[number]-'0'+second[number]-'0')+'0';

                cout << sum[number];
        }
        cout << endl;

        return 0;
        }

i know i have to do something with and if(sum[number] >=10) but i dont know where to go from there, any help would be great!

Draw a picture of what you're doing. You are putting the input number into array starting at index 0, putting highest valued digit there. What happens when you try to add two numbers of different length? You'll add the tens to the hundreds, or some such.

Also, you are reusing the variable count to track the length of each number. This causes you to loose track of how many digits are in the first number. You should have separate counters.

As to dealing with the carry, you need to start your adding at the units value, and have a carry variable that gets set at each addition. Simple way to avoid using an if( ) statement is to use division and modulus to set the carry and the place value.

Think about these things for a bit and get back to us.

#include <iostream>
#include <iomanip>

using namespace std;

int main()
{
	int count = 0, a;
	char first[99999], second[99999], sum[100000], carry;
	
	gets(first);
	count = strlen(first);
	gets(second);
	carry = '0';

	for(a = 0; a < count; a++)
    {
		sum[a] = (first[a] + second[a] + carry) - '0';
		carry = '0';

		if(sum[a] > ('9' - '0'))
		{
			sum[a] -= '9';
			carry = '1';
		}
		cout << sum[a];
    }
    return 0;
}

here is what I came up so far I can't get the

sum[number] -= '9';

to work, but if you do, please post the solution here. Thank you and your welcome!! :P

That's not all that's not working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.