An isdigit Equivalent?

This is a discussion on An isdigit Equivalent? within the C++ Programming forums, part of the General Programming Boards category; I'm working on a small program with an integer array, and I'm having problems with my if condition: Code: if(!isdigit(numbers[x])) ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    56

    An isdigit Equivalent?

    I'm working on a small program with an integer array, and I'm having problems with my if condition:
    Code:
    if(!isdigit(numbers[x]))
    {
         result = 0;
    }
    I'm using cctype and cstdlib libraries but is the compiler having a problem with me using this command on an integer array, and if so, what is an equivalent command?

    Thanks so much.

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    The <cctype> header is for C character types, and its isdigit function tests a character to see if it represents a digit. If you've got an integer array, it's always going to be filled with integers, so what are you asking?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Well, to be specific, I would like to return a value if there aren't any digits in the array element, because I'm working with more than one array and I'm using the same loop to loop through both arrays at the same time, and one array may be longer than the other.

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    If the array index is higher than the array allows, you get undefined behaviour. You can't do that. You have to check the index against the array's length.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Here is a snippet of what I'm talking about...
    Code:
    	for(x = 0; x <= maxLength+1; x++)
    	{
    /*		if((!isdigit(num1[x])) && (!isdigit(num2[x])))
    		{
    			temp1 = 0;
    			temp2 = 0;
    		}
    		else if(!isdigit(num1[x]))
    		{
    			temp1 = 0;
    			temp2 = (num2[x] - '0');
    		}
    		else if(!isdigit(num2[x]))
    		{
    			temp1 = (num1[x] - '0');
    			temp2 = 0;
    		}
    		else
    */
    		{
    			temp1 = (num1[x] - '0');
    			temp2 = (num2[x] - '0');
    		}
    
    		tempResult = temp1 + temp2 + carry;
    
    		if(tempResult > 9)
    		{
    			carry = tempResult / 10;
    			tempResult = tempResult &#37; 10;
    		}
    		else
    		{
    			carry = 0;
    		}
    
    		answer[x] = tempResult;
    
    		answer[x+1] = '\0';
    		num1[x] = '\0';
    		num2[x] = '\0';
    
    	}
    
    	answer[x] = carry;
    
    	if(carry == 0)
    	{
    		for(x = maxLength; x >= 0; x--)
    		{
    			cout << answer[x];
    		}
    		cout << endl;
    	}
    	else
    	{
    		for(x = 0; x <= maxLength+1; x++)
    		{
    			cout << answer[x];
    		}
    		cout << endl;
    	}
    Which seems to work great up until the end of the smaller array, so you can see I tried playing with the built-in isdigit() but the compiler isn't happy about it. Do you mean to say I should include the array lengths as counters as well, so that if the array length falls below 0, the the value 0 is returned?
    This is possible, but I'm hoping for a simpler command line.
    Last edited by marQade; 02-20-2008 at 05:57 PM. Reason: More stuff to say, said some stuff twice

  6. #6
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    I have fixed it! It is operational! Here is what I ended up doing:
    Code:
    	for(x = 0; x <= maxLength+1; x++)
    	{
    		if((x > ar1Length) && (x > ar2Length))
    		{
    			temp1 = 0;
    			temp2 = 0;
    		}
    		else if((x > ar1Length) && (x <= ar2Length))
    		{
    			temp1 = 0;
    			temp2 = (num2[x] - '0');
    		}
    		else if((x <= ar1Length) && (x > ar2Length))
    		{
    			temp1 = (num1[x] - '0');
    			temp2 = 0;
    		}
    		else
    		{
    			temp1 = (num1[x] - '0');
    			temp2 = (num2[x] - '0');
    		}
    
    		tempResult = temp1 + temp2 + carry;
    
    		if(tempResult > 9)
    		{
    			carry = tempResult / 10;
    			tempResult = tempResult % 10;
    		}
    		else
    		{
    			carry = 0;
    		}
    
    		answer[x] = tempResult;
    
    		answer[x+1] = '\0';
    		num1[x] = '\0';
    		num2[x] = '\0';
    
    	}
    
    	answer[x] = carry;
    
    	if(carry == 0)
    	{
    		for(x = maxLength; x >= 0; x--)
    		{
    			cout << answer[x];
    		}
    		cout << endl;
    	}
    I thought things would be more complicated but not too bad. Thanks so much!

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    It doesn't have to be quite so complicated. You could use something like this:
    Code:
    	for(x = 0; x <= maxLength+1; x++)
    	{
    		if(x > ar1Length)
    		{
    			temp1 = 0;
    		}
    		else {
    			temp1 = (num1[x] - '0');
    		}
    
    		if(y > ar2Length)
    		{
    			temp2 = 0;
    		}
    		else {
    			temp2 = (num2[x] - '0');
    		}
    
    		tempResult = temp1 + temp2 + carry;
    
    		if(tempResult > 9)
    		{
    			carry = tempResult / 10;
    			tempResult = tempResult % 10;
    		}
    		else
    		{
    			carry = 0;
    		}
    
    		answer[x] = tempResult;
    
    		answer[x+1] = '\0';
    		num1[x] = '\0';
    		num2[x] = '\0';
    
    	}
    
    	answer[x] = carry;
    
    	if(carry == 0)
    	{
    		for(x = maxLength; x >= 0; x--)
    		{
    			cout << answer[x];
    		}
    		cout << endl;
    	}
    I'm sure you could make it even more concise if you put your mind to it, by using one set of variables instead of temp1 and temp2 etc, or by using the ternary operator:
    Code:
    temp1 = (x > ar1Length) ? 0 : (num1[x] - '0');
    I know, I know. But I'm used to it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Registered User
    Join Date
    Nov 2007
    Posts
    56
    Cool code! Thanks for showing me that!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer equivalent to array notation
    By bekkilyn in forum C Programming
    Replies: 4
    Last Post: 12-06-2006, 08:22 PM
  2. isdigit woes
    By kryonik in forum C Programming
    Replies: 3
    Last Post: 10-14-2005, 06:10 PM
  3. Replies: 10
    Last Post: 08-18-2005, 12:17 AM
  4. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM
  5. Correct use of isdigit
    By DocDroopy in forum C Programming
    Replies: 3
    Last Post: 08-05-2002, 08:22 AM

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