• 02-15-2008
Hi. I am working on a stubs only miniature program for school concerning adding integers much too large to be stored as integers. What I have so far seems to work great up until I add 10 integers to 10 integers. Here is what I have so far, and kindly thank anyone who has time to give me a bit of advice. I apologize for the lack of documentation in this program - I haven't really gotten that far yet.

Code:

```const int MAXCHARS = 80; char numbers[MAXCHARS]; int result[MAXCHARS]; char num1[MAXCHARS]; char num2[MAXCHARS]; char op; int splitPoint; int opFinder = 0; int count1 = 0; int count2 = 0; int x; int ar1Length; int ar2Length; int maxLength; int temp1; int temp2; int tempResult; int carry1 = 0; int carry2; int main() {         cout << "This program adds or multiplies numbers" << endl                 << "much too large to be stored as integers." << endl                 << "Please refer to the example to enter your problem." << endl << endl                 << "(EXAMPLE) 100 + 10" << endl << endl                 << "Calculate: ";         cin.getline(numbers, MAXCHARS, '\n');         for(opFinder = 0; opFinder < MAXCHARS; opFinder++)         {                 if((numbers[opFinder] == '+') || (numbers[opFinder] == '*'))                 {                         splitPoint = opFinder;                         op = numbers[opFinder];                 }         }         while(isdigit(numbers[count1]))         {                 num1[count1] = numbers[count1];                 numbers[count1] = '\0';                 num1[count1+1] = '\0';                 cout << num1[count1] << endl;                 count1++;         }                 ar1Length = count1-1;         num1[count1] = '\0';         count1 = 0;         count2 = splitPoint + 1;         while(numbers[count2] != '\0')         {                 if(numbers[count2] == ' ')                 {                         count2++;                 }                 num2[count1] = numbers[count2];                         numbers[count2] = '\0';                 num2[count1 + 1] = '\0';                 count1++;                 count2++;         }         ar2Length = count1-1;         num2[count1] = '\0';         if(ar1Length > ar2Length)         {maxLength = ar1Length;}         else         {maxLength = ar2Length;}         cout << "You have entered: " << endl                 << setw(maxLength+2) << setfill(' ') << num1 << endl                 << op << setw(maxLength+1) << setfill(' ') << num2 << endl;         for(x = 0; x <= maxLength+2; x++)         {cout << "-";}         cout << endl;         x = 0;         while((num1[x] != '\0') || (num2[x] != '\0'))         {                 if((num1[x] == '\0') && (num2[x] == '\0'))                 {break;}                 else if(num1[x] == '\0')                 {temp1 = 0;}                 else if(num2[x] == '\0')                 {temp2 = 0;}                 else                 {                         temp1 = atoi(&num1[x]);                         temp2 = atoi(&num2[x]);                 }                                 if((temp1+temp2) > 9)                 {                         tempResult = (temp1 + temp2) &#37; 10;                         carry1 = (temp1 + temp2) / 10;                 }                 tempResult = temp1 + temp2 + carry2;                 result[x] = tempResult;                 num1[x] = '\0';                 num2[x] = '\0';                 result[x+1] = '\0';                 cout << result[x] << endl;                 temp1 = 0;                 temp2 = 0;                 carry2 = carry1;         }         return 0; }```
• 02-15-2008
dwks
Hopefully you have some header files, such as iostream for cout and cctype for isdigit(), and whatever atoi() is in -- cstdlib, I think. I never use it, because of its nonexistent error checking. I suggest you look at strtol() at some point in the future for better error checking.

I'm also wondering if you ought to be incrementing x in your last while loop there.
• 02-15-2008
Daved
>> ar1Length = count1-1;
>> ar2Length = count1-1;
Are you sure those are right? I don't think you need the -1's there.

What if the user inputs two spaces in a row after the plus sign?

>> while((num1[x] != '\0') || (num2[x] != '\0'))
You only set the character after the last digit in num1 and num2 to null. So what if you're adding 123456 + 12? Then the while might not finish. If you want to make that the control, you should ensure that all values of x will be null after the number in the arrays.

>> if((num1[x] == '\0') && (num2[x] == '\0'))
That's unnecessary given the while condition right above it.

>> else if(num1[x] == '\0')
>> else if(num2[x] == '\0')
If either of these two run, then temp2 or temp1 will not get set for that iteration.

>> temp1 = atoi(&num1[x]);
>> temp2 = atoi(&num2[x]);
Why the & there? I assume you're trying to make atoi happy since it expects a null terminated array, but then you'll get the whole number in the array, not a single digit. You're just trying to convert a digit, so use this: temp1 = num1[x] - '0';

>> tempResult = temp1 + temp2 + carry2;
Why do you overwrite the value of tempResult that you just calculated in the if above it?

Are you sure this working with any input? Try to look at some of those issues (and the x increment dwks pointed out), verify that they really are problems, and see if you can come up with solutions for them.
• 02-15-2008
dwks
. . . and iomanip, for setfill().

You don't really need to make those variables global, since you only have one function. Although, again since you only have one function, it probably doesn't make too much difference.

And since Elysia's going to say it, I'll say it first: your indentation is a little hard to read, where you have both curly braces on one line.

I don't think you need quite so many variables. You should be able to use just one carry variable, for instance.

Code:

```        while(isdigit(numbers[count1]))         {                 num1[count1] = numbers[count1];                 numbers[count1] = '\0';                 num1[count1+1] = '\0';                 cout << num1[count1] << endl;                 count1++;         }                 ar1Length = count1-1;  // what if count1 is zero?         num1[count1] = '\0';```
• 02-15-2008
cpjust
It would definitely be easier to read if you broke it into several functions which each do one specific task.
Switching to STL strings & vectors would probably make things easier too.
• 02-18-2008
Thank you for all your comments. I've fixed up some of the stuff from my stubs program, and got it working okay as far as that. Here is the code for the stubs only with NO error-trapping and NO provision for arrays of different lengths. I tried to convert this into a three part program, with a header file, definition file, and main function, adding some complexity, but I'm having some trouble adding arrays of different sizes. I've attached the three files for your perusal. Thanks again.
Code:

```#include <iostream> #include <iomanip> #include <string> #include <cctype> #include <cstdlib> using namespace std; int main() { const int MAXCHARS = 80; char numbers[MAXCHARS]; int result[MAXCHARS]; char num1[MAXCHARS]; char num2[MAXCHARS]; char op; int splitPoint; int opFinder = 0; int count1 = 0; int count2 = 0; int x; int ar1Length; int ar2Length; int maxLength; int temp1; int temp2; int tempResult; int carry = 0;         cout << "This program adds or multiplies numbers" << endl                 << "much too large to be stored as integers." << endl                 << "Please refer to the example to enter your problem." << endl << endl                 << "(EXAMPLE) 100 + 10" << endl << endl                 << "Calculate: ";         cin.getline(numbers, MAXCHARS, '\n');         for(opFinder = 0; opFinder < MAXCHARS; opFinder++)         {                 if((numbers[opFinder] == '+') || (numbers[opFinder] == '*'))                 {                         splitPoint = opFinder;                         op = numbers[opFinder];                 }         }         while(isdigit(numbers[count1]))         {                 num1[count1] = numbers[count1];                 numbers[count1] = '\0';                 num1[count1+1] = '\0';                 cout << num1[count1] << endl;                 count1++;         }                 ar1Length = count1;         num1[count1] = '\0';         count1 = 0;         count2 = splitPoint + 1;         while(numbers[count2] != '\0')         {                 if(numbers[count2] == ' ')                 {                         count2++;                 }                 num2[count1] = numbers[count2];                         numbers[count2] = '\0';                 num2[count1 + 1] = '\0';                 count1++;                 count2++;         }         ar2Length = count1-1;         num2[count1] = '\0';         if(ar1Length > ar2Length)         {maxLength = ar1Length;}         else         {maxLength = ar2Length;}         cout << "You have entered: " << endl                 << setw(maxLength+2) << setfill(' ') << num1 << endl                 << op << setw(maxLength+1) << setfill(' ') << num2 << endl;         for(x = 0; x <= maxLength+2; x++)         {cout << "-";}         cout << endl;         x = 0;         while(x <= maxLength+2)         {                 if(num1[x] == '\0')                 {                         temp1 = 0;                 }                 else if(num2[x] == '\0')                 {                         temp2 = 0;                 }                 else                 {                         temp1 = (num1[x] - '0');                         temp2 = (num2[x] - '0');                 }                 tempResult = temp1 + temp2 + carry;                                 if((tempResult) > 9)                 {                         tempResult = (temp1 + temp2 + carry) % 10;                         carry = (temp1 + temp2) / 10;                 }                 else                 {                         carry = 0;                 }                 result[x] = tempResult;                 num1[x] = '\0';                 num2[x] = '\0';                 result[x+1] = '\0';                 temp1 = 0;                 temp2 = 0;                 x++;         }         cout << " ";         for(count2 = maxLength; count2 >= 0; count2--)         {                 cout << result[count2];         }         cout << endl;         return 0; }```
• 02-18-2008