# Can't Find Error in Code

• 03-29-2007
DarkDot
Can't Find Error in Code
I have a program and basically my problem is that i'm trying to increment it using an increment function i made. The function just increments the number by 1. It increments all numbers correctly unless it is a single digits number this is where i have my problem. I made a test file and it doesn't increment 0 to 1. i'll post my code for my increment, set and print fucntion. then the code from the test file. I've spent about an hour or so trying to find the problem and can't.
Code:

```Set(string num) { int i; if (num[0] == '-')         {                 positive = false;                 num.erase(0,1);                 numDigits = num.length();                 digits = new int[numDigits];                        for(i=1;i<=numDigits;i++)                 {                         digits[numDigits-i] = (num[numDigits-i]-'0');                 }         } else         {                 positive = true;                 numDigits = num.length();                 digits = new int[numDigits];                        for(i=1;i<=numDigits;i++)                 {                         digits[numDigits-i] = (num[numDigits-i]-'0');                                        }         } }   Print() { int i; if(positive == false) {         cout << "-";         for(i=0;i<=numDigits-1;i++)         {                 cout << digits[i];         } } else if(positive == true) {         for(i=0;i<=numDigits-1;i++)         {                 cout << digits[i];         } } } Increment() { int i; int test; //positive case with no need to reallocate digits. if(positive == true) {                test = 9 * numDigits;         if(test == 9)         {                 delete[] digits;                 digits = new int[numDigits+1];                 for(i=1;i<=numDigits;i++)                 {                         digits[numDigits-i] = 0;                 }                 ++digits;         }         else if(digits[numDigits-1] == 9 && test != 9)         {                 for(i=1;i<=numDigits;i++)                 {                         while(digits[numDigits-i] == 9)                         {                                 digits[i] = 0;                                 ++digits[i-1];                         }                 }         }         else if(digits[numDigits-1] != 9 && test !=9)         {                 ++digits[numDigits-1];                        } } }```
now the code from the test file(its just my main file)
Code:

``` // ********** BEGIN TESTS ********** //     // ========== Set() tests ========== //     // Test the default value     cout << "Expecting 0:" << endl;     a.Print();     cout << endl;     // Test setting the value 0: should print 0     cout << "Expecting 0:" << endl;     intArray[0].Set("0");     intArray[0].Print();     cout << endl;     // ========== Increment() tests ========== //     // Test incrementing 0: should print 1     cout << "Expecting 1:" << endl;     intArray[0].Increment();     intArray[0].Print();     cout << endl;```
• 03-29-2007
Daved
>> ++digits;
What is this supposed to do? It is actually incrementing an array pointer, it is not incrementing the integer store in the array.

I'm not sure why you need special case code for a single digit number. It should work the same as a number with multiple digits.

>> test = 9 * numDigits;
I don't think this is what you want to do here. I think you should look at this again and see if it is doing what you think it is.
• 03-29-2007
DarkDot
++digits..i thought that would increment the whole thing as in the first integer...it seems to be working, and the test = 9*numDigits, i mean i made that so it would be a sure way to tell if the whole digits array is full of 9's..thats the only thing i could think of.
• 03-29-2007
Why are you using a string to hold your number value?
• 03-29-2007
Daved
>> it seems to be working
Are you sure. I thought your problem was that single digits aren't incrementing. That code is only run on single digits, and it is in fact not incrementing the number. digits is an array of integers. You want to increment the first integer in the array, not the array pointer.

>> i made that so it would be a sure way to tell if the whole digits array is full of 9's.
I figured that's what you were thinking, but all it does is multiply 9 times the number of digits. So if you have 3 digits, then test is 27. If you have one digit, test is 9. To check for all 9's, you should look at all the digits and if any of them are not a 9, then you know they are not all 9's.

>> Why are you using a string to hold your number value?
There is no string holding the number value except the one passed into Set. But that has to be a string because presumably the point of this class is to hold arbitrarily large numbers, so you cannot use int, long, etc, which have finite maximums.
• 03-29-2007
DarkDot
i need to fix that first case...but that last case was supposed to handle numbers that aren't all 9's and don't have a 9 in the numDigits-1 spot.
• 03-29-2007
Daved
What happens if numDigits is 1? What is test? Which if block will the code enter? Not the last one like you think.