Using escape sequences as user inputs

This is a discussion on Using escape sequences as user inputs within the C Programming forums, part of the General Programming Boards category; Hello again, I need to find out a way to make it so the program reads characters from keyboard and ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    11

    Using escape sequences as user inputs

    Hello again,

    I need to find out a way to make it so the program reads characters from keyboard and watches for escape sequences. Characters are easy to input but what are the variable data types for escape sequences? Here's my code sample

    Code:
    void main (void)
    {
    
    char char_input;
    
    printf ("\nPlease enter a character:  ");
    scanf ("%c", &char_input);
    
    switch (char_input)
    	{
    	case '\t':
    		printf ("\n\nYou have entered 'TAB'.");
    		break;
    	case '\r':
    		printf ("\n\nYou have entered 'ENTER'.");
    		break;
    	default:
    		printf ("\n\nYou have entered '%c'.", char_input);
    		break;
    	}
    }
    Obviously, data type char would work for any character keys but seems like they won't work for escape sequences. I'm guessing I'd need some kind of new data type but not sure what it is.

    Thanks.

  2. #2
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    Unfortunatly, you have to work it out yourself.
    // Gliptic

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    11
    Originally posted by gliptic
    Unfortunatly, you have to work it out yourself.


    I understand the board has a rule on asking other people to do your work but it's not what I'm asking.

    I'm assuming I'll need a new data type because it's the only possible error I can figure out. I've already tried "working it myself", otherwise I wouldn't be asking here

    Also tried looking into the tutorials on this website but it doesn't seem to cover that.

    Since I have no access to books, I'm also out of luck on that.

    All I'm asking is if it's a new data type that I'm missing, and if so, what it might be. I can pretty much figure out everything else.

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    164
    I think you understood me wrong. There isn't any way of inputing escape sequences using scanf(). You have to input them as strings and interpret them yourself.
    // Gliptic

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Actually, you can, but there isn't a good ansi way to do it. In dos you can use 'kbdhit()' to find when a key has been hit and then read it from the keyboard buffer. This however is not an ANSI function and not all compilers have or support it. You could do something like:
    Code:
    #include <stdio.h>
    int main ( void )
    {
        int c='a';
    
        while( c != 'q' && c != 'Q' )
        {
            printf("Hit a key, then enter. Q to quit: ");
    
            c = getchar( );
            getchar( ); // ignore the enter key that follows the first call
    
            switch( c )
            {
                case '\t': printf("You entered a tab.\n"); break;
                default:
            }
        }
    
        return 0; /* NOTE: MAIN ALWAYS RETURNS AN INT!!! */
    }
    Other than that, just reading a single key HIT is not an easy thing to do.

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie Help: Currency Converter
    By Ashfury in forum C Programming
    Replies: 10
    Last Post: 11-06-2005, 01:21 PM
  2. Hello and can someone help me??..
    By GGrrl in forum C++ Programming
    Replies: 3
    Last Post: 03-20-2003, 12:08 PM
  3. ANSI Escape Sequences OR Scan of keyboard
    By Samppa in forum Linux Programming
    Replies: 3
    Last Post: 10-24-2001, 01:15 PM
  4. Escape sequences in VC++
    By emilyh in forum Windows Programming
    Replies: 7
    Last Post: 09-26-2001, 08:02 AM
  5. Escape Sequences
    By Me-Again-Again in forum C Programming
    Replies: 3
    Last Post: 09-05-2001, 07:24 AM

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