Can't Find Error in Code

This is a discussion on Can't Find Error in Code within the C++ Programming forums, part of the General Programming Boards category; I have a program and basically my problem is that i'm trying to increment it using an increment function i ...

  1. #1
    Registered User
    Join Date
    Mar 2007
    Posts
    109

    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;

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> ++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.

  3. #3
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    ++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.

  4. #4
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Why are you using a string to hold your number value?
    Don't quote me on that... ...seriously

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    >> 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.

  6. #6
    Registered User
    Join Date
    Mar 2007
    Posts
    109
    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.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,319
    What happens if numDigits is 1? What is test? Which if block will the code enter? Not the last one like you think.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. code to find th square root of a number
    By CODY21 in forum C++ Programming
    Replies: 34
    Last Post: 10-29-2010, 09:27 AM
  2. Replies: 12
    Last Post: 06-08-2005, 11:23 AM
  3. << !! Posting Code? Read this First !! >>
    By kermi3 in forum C# Programming
    Replies: 0
    Last Post: 10-14-2002, 01:26 PM
  4. Code to find the day type of day in the year?
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 04-01-2002, 07:58 PM
  5. << !! Posting Code? Read this First !! >>
    By biosx in forum C++ Programming
    Replies: 1
    Last Post: 03-20-2002, 11:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21