Hi all, I'm new here...
I am a beginner in C++, I need your helping hands in my classwork...
The programme needs to use two string variables to store the inputs and reads two non-negative integers and reports their sum. There is no limit on input number.
The string will hold a string representation of a number. Since it's a string and not an actual number, you will not be able to simply add with with + operator. Instead you will have to extract each digit from the string and do the addition "manually".
Think back to first grade math. To add two numbers together, you add the "ones" together, then you add the "tens", etc, etc. That is what your code will have to do here. You'll have to add each digit one at a time.
Thanks so much Disch, actually I knew it had to do it manually but I had no ideas how to do so.
May you give me some example by code so that I could have a brief idea on it??
Thanks.
Here's a simple example with 1 digit numbers to help show how to convert:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
string a = "1";
string b = "6";
// now we want to "extract" a digit from each string
int aa = a[0]; // extracts a single digit
aa -= '0'; // subtract the ASCII code for '0', this "converts" it from a character to an integer
// here, aa == 1
// same thing wtih b:
int bb = b[0] - '0';
// now we can add them together:
int sum = aa + bb;
// here, sum == 7
Your code will need to do the same thing, but will need to sum each digit in a loop (keep in mind, if a single digit sums to 10+, you will have to add the carry to the next digit)