adding two user input arrays
For this program I have to be able to input two positive arrays up to 20 digits long and add them together, I think I have the input part right I am just having difficulty adding the two arrays together and outputting the new sum array...any help or advice would be great, 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[20];
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 << first[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 << second[number];
cout << endl;
number=0;
for(number=0;number<20;number++)
{
sum[number] = first[number] + second[number];
}
cout << sum[number];
cout << endl;
return 0;
}
Re: adding two user input arrays
Stick some code tags on that to preserve indenting.
Re: adding two user input arrays
For this program I have to be able to input two positive arrays up to 20 digits long and add them together, I think I have the input part right I am just having difficulty adding the two arrays together and outputting the new sum array...any help or advice would be great, here is what i have so far:
Quote:
Originally Posted by
theflash
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int count = 0, number;
char first[20], second[20], sum[20];
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 << first[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 << second[number];
cout << endl;
number=0;
for(number=0;number<20;number++)
{
sum[number] = first[number] + second[number];
}
cout << sum[number];
cout << endl;
return 0;
}
Re: adding two user input arrays
Two areas that stick out as problematic:
1) The inputting of the array values... why are you comparing the input to '\n'????
2) The output of the array sums... you are only outputting sum[20], which is beyond the size of the array.