Here is my task: Write a program that inputs two positive integers of, at most, 20 digits and outputs the sum of the numbers. If the sum of the numbers has more than 20 digits, output the sum with an appropriate message. Your program must, at least, contain a function to read and store a number into an array and another function to output the sum of the numbers. (Hint: Read numbers as strings and store the digits of the number in the reverse order.)
Ive pretty much gotten things fixed, its just that my output is printing each index's sum rather than a whole number with multiple digits. Also, I tried to add code to deter the user from typing over 20 characters but I get an error regarding the string variable not being able to used in this statement: if (user_str1>20)
int read_strings(int arr1[], int arr2[])
{
string user_str1;
string user_str2;
cout << "enter a positive integer of, at most, 20 digits " << endl;
cin >> user_str1;
A suggestion, use separate functions for the addition and output.
Addition needs to progress starting from the least-significant digit to the most-significant, while output needs to start from the most significant digit. That's a fundamental incompatibility.
Thanks, but my assignment forces me to follow certain guidelines. Also, how can i get the output to print 1 single integer, rather than the sum at each index.
Thanks, but my assignment forces me to follow certain guidelines.
Nevertheless, the fundamental incompatibility I referred to remains. At the very least you should separate out the processing of the two activities, even if they need to be contained in a single function.
Actually, the original post states,
"Your program must, at least, contain a function to read and store a number into an array and another function to output the sum of the numbers."
It says you should use at least two functions, it doesn't prohibit the use of other functions (in fact I'd consider it almost invites their use).
Also, how can i get the output to print 1 single integer, rather than the sum at each index.
Remove the cout << endl; from inside the loop, and just put a single newline after the loop has ended.
By the way, the storing of the digits in your arrays here:
Well, the loop steps through the characters of the string user_str1 in reverse order. It starts from the last character and proceeds one at a time until it reaches the first character. That's ok.
But what about the array arr1 ? The loop uses the same subscript ind2 to access that array too, so it too is receiving the digits in reverse order, starting from the last, and ending at the first. All that achieves is in effect an identical copy (if we ignore the conversion from character to integer for a moment).