All right, so this time I have an assignment to add two positive integers up to 20 digits long and output the two entries along with the sum. To this point I've been able to get the program to compile but the output is out of whack. Here's what I've got, and an example of the output:
Code:
#include <iostream>
using namespace std;
int main()
{
char augend[20] = {0}, addend[20] = {0}, sum[21], temp[20];
int aug = 0, add = 0, sumc = 20, tempc = 0;
int carry = 1;
cout << "This program adds two positive integers up to 20 digits long."
<< "\n\n";
cout << "Enter the augend: ";
cin >> noskipws >> temp[0];
while(temp[tempc]!='\n')
{
tempc++;
cin >> temp[tempc];
}
cout << "Enter the addend: ";
cin >> noskipws >> addend[0];
while(addend[add]!='\n')
{
add++;
cin >> addend[add];
}
for(aug=tempc; aug>=0; aug--, tempc--)
{
augend[aug] = temp[tempc];
}
for(aug=19, sumc=20; aug>=0, sumc>=0, add>=0; aug--, sumc--, add--)
{
if(augend[aug] + addend[add] >= 10)
{
sum[sumc] = augend[aug] + addend[add] - 10;
sum[sumc - 1] = carry;
}
else
{
sum[sumc] = augend[aug] + addend[add];
}
}
temp[tempc]='\0';
addend[add]='\0';
cout << "\n\n" << temp << "+" << addend << "=" << sum << ".\n";
return 0;
}
And a sample output:
Code:
This program adds two positive integers up to 20 digits long.
Enter the augend: 2021
Enter the addend: 395
2021
+395
= R@.
All right, what's screwy here? I have a feeling part of it is due to the addition of the two arrays (augend and addend) - I'm not confident I've done that right. Advice/assistance?