The first of the two solutions that failed:
int main() {
char arry[2][21];
int arry2[21] = {0};
int row,col;
for (row = 0; row < 2; row++) {
for (col = 0; col < 20; col++) {
cin >> arry[row][col];
}
}
for (col = 20; col >= 0; col--) {
arry2[col] += (static_cast<int>(arry[0][col]) - 48) + (static_cast<int>(arry[1][col]) - 48);
//carry the one
if (arry2[col] >= 10) {
arry2[col] -= 10;
arry2[col-1] += 1;
}
}
for (col = 0; col < 20; col++) {
cout << arry2[col];
}
return 0;
}
This one works in that it will output the correct number. There's three problems, though.
1) It forces the input to be entered in individual digits
2) It forces the user to input a total of 40 digits, rather than that just being the max
3) If the user inputs numbers that will end in the last digit being a carried one (ex: 50000000000000000000 + 50000000000000000000) I'll get an error stating: "Stack around the variable 'arry2' was corrupted"
So I tried to come up with this one:
int main() {
char arry[2][21];
int length1,length2;
int row, col;
string str1, str2;
cin >> str1 >> str2;
length1 = str1.size();
length2 = str2.size();
for (row = 0; row < 2; row++) {
for (col = 20; col > 0; col--) {
if (row == 0) {
arry[row][col] = str1.substr(length1, 1);
length1--;
} else {
arry[row][col] = str2.substr(length2, 1);
length2--;
}
}
}
return 0;
}
Before I finished, I learned that a char variable apparently can't take a character from a string. At least, the way I tried it.
I am lost.

New Topic/Question
Reply



MultiQuote




|