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.
now the code from the test file(its just my main file)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]; } } }
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;



LinkBack URL
About LinkBacks


