Adding two numbers

Write your question here.

Hello, I am trying to add two numbers that the user puts into arrays ("a" and "b") The problem is with the carryovers - I can't get that to work. For example: if I try to add 1234 and 5678 I end up with "681012" as the result. Someone told me to read the numbers in the reverse order but I have no idea of how to accomplish that. The area that actually adds the two numbers is in the function definition "Add" near the bottom. Thanks!

[code]
Put the code you need help with here.
Code Follows:


#include <iostream>
#include<cstring>
#include<string>


using namespace std;

const int maxDigits = 20;

void zeroArray(int[], int[], int[], int);
void inputInt(int [], int&);
void add(int[], int, int[] , int, int[], int& );
void outputInt(int [], int);

int main()
{

int a[maxDigits], b[maxDigits], sum[maxDigits];

int sizeA, sizeB, sizeSum;

char answer;

do
{
zeroArray(a, b, sum, maxDigits);
inputInt(a, sizeA);
inputInt(b, sizeB);
add(a, sizeA, b, sizeB, sum, sizeSum);

cout << "The sum of \n";
outputInt(a, sizeA);
cout << " and ";
outputInt(b, sizeB);
cout << " is ";
outputInt(sum, sizeA);
cout << "\n\n";
cout << "Do you want to run this program again? (y or n): ";
cin >> answer;
cout << "\n\n";
}
while (answer == 'y' || answer == 'Y');

return 0;
}
void zeroArray(int a[], int b[], int sum[], int maxDigits)
{
for(int i = 0; i < maxDigits; i++)
a[i] = 0;
for(int i = 0; i < maxDigits; i++)
b[i] = 0;
for(int i = 0; i < maxDigits; i++)
sum[i] = 0;
}
void inputInt(int ab[], int& sizeAB)
{
char digit[maxDigits];
char number;
int i = 0;
cout << "Please enter a positive integer - no more than 20 digits: ";

cin.get(number);

while (isdigit(number) && i < maxDigits)
{
digit[i] = number;
i++;
cin.get(number);
}
sizeAB = i;
int j = 0;

while (i > 0)
{
i--;
ab[i] = digit[i] - '0';
j++;
}
}
void add(int a[], int sizeA, int b[], int sizeB,
int sum[], int& sizeSum)
{
int i;
int carry = 0, value = 0;

for(i = 5; i >=0; i--)

value = a[i] + b[i];
cout << value;
if (value > 9)
{
sum[i] = value - 10 + carry;
//sum[i-1]+1;
carry = 1;
}
else if (sum[i] <= 9)
{
sum[i] = value + carry;
carry = 0;
}
if ((a[i] + b[i])/20 > 0)
{
cout << "Error: integer overflow in the sum.\n"
<< endl;
}
}
void outputInt(int sum[], int sizeA)
{
for (int i = 0; i < sizeA; i++)
cout << sum[i];
}
Topic archived. No new replies allowed.