Thread: escape sequence

  1. #1
    Unregistered
    Guest

    escape sequence

    I would like to make an error appear when a user hits enter instead of entering data first (ie. leaving the line blank). I have tried
    line[i] == '' generates an error
    line[i] == ' ' only works if they hit the spacebar first
    line[i] == '\0' or EOF or '\r' or '\f' none of which work...

    my first instinct is to use '\n' but obviously that wouldnt work.

    So if there isnt an escape sequence for this, what would be the best way? Count the number of characters and if that number > 0 then error?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    How are you reading the input in the first place?

    If you're using fgets(), then the answer is easy

    Code:
    char buff[100];
    if ( fgets( buff, sizeof(buff), stdin ) != NULL ) {
      if ( buff[0] == '\n' ) {
        // user just pressed return
      } else {
        // mmm, something's in the buffer, better see what they typed
      }
    }

  3. #3
    Unregistered
    Guest

    passed in

    yes it is getting passed in via fgets but then sent to a validate function where I check to make sure the input is within all the paramaters (ie... (a - n) etc)

    If I must keep it in main then I will but I would really like to have this in the function where it would make more sense.

    thats where this is used
    if( line[i] == '\n' )
    return TRUE; //this needs to be here or else everything entered returns as FALSE

  4. #4
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    ok Im still messing with it...

    ok did get salems code to work, but is there anyway to get the same thing accomplished within the function?

    here is the full function:
    Code:
     static int valid( char array[], int maxnum )
    {   
    	int i = 0, decCount = 0, negCount = 0;
    
    	for( i = 0; i < maxnum; i++)
    	{
    		if( array[i] == '\n' )
    			return BOOL_TRUE;
    
    		if( array[i] == NULL )  // **THIS IS THE PROBLEM.**
    			return BOOL_FALSE;
    
    		if(( array[i] < '0' || array[i] > '9' ) && array[i] != '.'&& array[i] != '-')
    			return BOOL_FALSE;
    
    		if( array[i] == '-' && i > 0) // '-' can only be first element of array
    			return BOOL_FALSE;
    		
    		if( array[i] == '.' )
    			decCount++;
    		if( decCount > 1)  // only allow 1 decimal
    			return BOOL_FALSE;
    	}
    
    	return BOOL_TRUE;
    }

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >if( array[i] == NULL ) // **THIS IS THE PROBLEM.**
    what are you trying to test for here? did you mean
    >>if( array[i] == '\0' )
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: ok Im still messing with it...

    i *think* i understand what you're asking. see modified code

    Code:
    static int valid( char array[], int maxnum )
    {   
    	int i = 0, decCount = 0, negCount = 0;
    
    	for( i = 0; i < maxnum; i++)
    	{
    		if( array[i] == '\n')
    			if (i > 0)
    				return BOOL_TRUE;
    			else
    				return BOOL_FALSE;
    		
    		if( array[i] == NULL )  // **THIS IS THE PROBLEM.**
    			return BOOL_FALSE;
    		
    		if(( array[i] < '0' || array[i] > '9' ) && array[i] != '.'&& array[i] != '-')
    			return BOOL_FALSE;
    
    		if( array[i] == '-' && i > 0) // '-' can only be first element of array
    			return BOOL_FALSE;
    		
    		if( array[i] == '.' )
    			decCount++;
    		if( decCount > 1)  // only allow 1 decimal
    			return BOOL_FALSE;
    	}
    
    	return BOOL_TRUE;
    }

  7. #7
    Registered User
    Join Date
    Jul 2002
    Posts
    37

    THATS IT!

    Sorry if I was too vague.

    Thanks for your help!
    Ryan

    Good judgement comes from experience, and experience comes from bad judgement.
    - Fred Brooks

  8. #8
    Registered User moi's Avatar
    Join Date
    Jul 2002
    Posts
    946

    Re: THATS IT!

    Originally posted by RyeDunn
    Sorry if I was too vague.

    Thanks for your help!
    the problem was of course, that the \n is always at the end, whether or not there's stuff before it.

  9. #9
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: Re: THATS IT!

    Originally posted by moi


    the problem was of course, that the \n is always at the end, whether or not there's stuff before it.
    .... unless fgets() reads a buffer full of data, in which case the last character won't be \n.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Immediate programming help! Please!
    By xMEGANx in forum C++ Programming
    Replies: 6
    Last Post: 02-20-2008, 12:52 PM
  2. Weird problem: error C2017: illegal escape sequence
    By h3ro in forum C++ Programming
    Replies: 4
    Last Post: 01-14-2008, 07:29 PM
  3. Escape sequence for &
    By 3saul in forum C Programming
    Replies: 10
    Last Post: 02-28-2006, 10:01 PM
  4. Escape sequence of interest?
    By wickedclownz in forum C++ Programming
    Replies: 2
    Last Post: 06-19-2003, 09:28 AM
  5. escape sequence "\ooo" and "\xhhh"
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-01-2001, 09:30 PM