Code Compacting

This is a discussion on Code Compacting within the C Programming forums, part of the General Programming Boards category; ok this is a very easy one that i just cant really get my head around at this late hour ...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    32

    Red face Code Compacting

    ok this is a very easy one that i just cant really get my head around at this late hour of the nite. i simply want to compact this code so as it will fit on the screen over maybe 2 or 3 lines. heres the code:

    Code:
          if(((fptr->code[a][0] >= '0') && (fptr->code[a][0] <= '9')) && ((fptr->code[a][1] >= '0') && (fptr->code[a][1] <= '9')) && ((fptr->code[a][2] >= '0') && (fptr->code[a][2] <= '9')) && ((fptr->code[a][3] >= '0') && (fptr->code[a][3] <= '9')))
          {
             check = TRUE;
          }

    i basically want it to look like this:
    Code:
    if(((fptr->code[a][0] >= '0') && (fptr->code[a][0] <= '9')) &&
    ((fptr->code[a][1] >= '0') && (fptr->code[a][1] <= '9')) && 
    ((fptr->code[a][2] >= '0') && (fptr->code[a][2] <= '9')) && 
    ((fptr->code[a][3] >= '0') && (fptr->code[a][3] <= '9')))
    {
       check = TRUE;
    }
    thanks for everyones help.

  2. #2
    Slave MadCow257's Avatar
    Join Date
    Jan 2005
    Posts
    735
    Actually, you solved the problem yourself
    The second code should work.

    Consider this simple example (which compiles without error)
    Code:
    #include <stdio.h>
    
    int main()
    {
    	int a = 0;
    	int b = 0;
    	if (a == b && b == a && 
    		a == b && b == a && 
    		a == b && b == a)
    		printf("Success");
    	return 0;
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Congratulations, you've answered your own question by hitting enter once in a while. So what again was your question?

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Registered User
    Join Date
    Mar 2005
    Posts
    32
    thanks so much, i thinks i might need some sleep very soon.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    How about this?
    Code:
    for (i=o;i<4;i++:)
    {
       if ((fptr->code[a][i]>= '0') && (fptr->code[a][i]<='9'))
          check=true;
    };

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Let's see...
    Code:
    if( isdigit( code[a][0] ) && isdigit( code[a][1] )
    &&  isdigit( code[a][2] ) && isdigit( code[a][3] ) )
    Ok, some one else's turn.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    My earlier code isn't correct because it would give a true result even if not all conditions were met.
    Code:
    for (i=o;i<4;i++:)
    {
       if ((fptr->code[a][i]>= '0') && (fptr->code[a][i]<='9'))
          check=TRUE;
       else
          {
             check=FALSE;
             break;
          }
    };

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Quantum1024
    My earlier code isn't correct because it would give a true result even if not all conditions were met.
    Code:
    for (i=o;i<4;i++:)
    {
       if ((fptr->code[a][i]>= '0') && (fptr->code[a][i]<='9'))
          check=FALSE;
       else
          check=FALSE;
    };
    1) That's the letter o, not the number 0. ( "Oh", not "Zero" ) Which in this case, had better be variable declared some place, otherwise this won't compile. But I'll assume it's a typo, and that you meant zero.

    2) So now instead it always returns FALSE.

    3) This semi-colon isn't needed.

    [edit]
    4) Your edit beat my post, in which you corrected bullet point #2.
    [/edit]

    Quzah.
    Last edited by quzah; 03-24-2005 at 07:47 AM.
    Hope is the first step on the road to disappointment.

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    847
    LOL I need to sleep too

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 08:34 AM
  2. Values changing without reason?
    By subtled in forum C Programming
    Replies: 2
    Last Post: 04-19-2007, 11:20 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM

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