Thread: Input Chunking: How to

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    4

    Input Chunking: How to

    Hello All!

    I was wondering if anyone knows of a way that is similar to JAVA's system.in.skip(#); command in C, or if there isn't, is there a way to get around unwanted char inputs by the user in a scanf("%c",&option) line?

    thanks for your time and help.

  2. #2
    The Artful Lurker Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    Hi Moose,

    I'm not familiar with Java (ask Iain how lucky that makes me). Would you give an example of what you mean by "unwanted char inputs"? If you want to remove certain characters from a string, I would iterate through it. If you want to see if the value in a single character is valid, just test the value.

    I suspect I'm not fully understanding your question :)

    Thanks.
    Jason Deckard

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There are ways to scan and ignore:

    %d == read a number
    %*d == read, but ignore a number

    The same applies for other values. Additionally, you can use:

    %[abc] == read these values only
    %[^abc] == read anything but these

    Or, you can just scan input a character at a time.

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

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    4
    Well, I'm currently not getting the response I'd like, so here's an example of what I'm trying to do...

    i.e.

    "Please Enter a Character:"
    (User enters-->)abc

    "You Entered: a"

    (I'm trying to get rid of all the excess garbage that the user enters, including the '\n'(return line) character. But still keeping the 'a'.

    I've tried the following: ("%c%c",&option).. but that doesn't seem to help really.. just screws up later by not allowing ANY other character to be inputted.. as it assumes its an invalid character... blah blah balh ..(its the way my code's set up).

  5. #5
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    There's a couple ways to do this...

    First, you can make a FLUSH macro to flush the input buffer...
    Code:
    #define FLUSH for (;getchar() != '\n';)
    
    // ... Somewhere in the file, we want to get input...
    printf ("Gimme Input.\n");
    scanf ("%c", &myChar);
    FLUSH;
    // Now myChar has the first letter, and everything else that was
    //  in the buffer has been trashed by FLUSH
    Second, you can just grab the string with gets() and process that string...
    Code:
    char tempInput[80];
    printf ("Gimme Input.\n");
    gets (tempInput);
    sscanf (tempInput, "%c", &myChar);
    Incidentally, even though gets will generally work, provided your array is large, it is much safer to use fgets().

    Finally, there's the conversion specifiers. I'm not too sure about this one, but I think this will do the trick...
    Code:
    printf (Gimme Input.\n");
    scanf ("%c%*[^'\n']%*c", &myChar);
    That format string basically says this....
    %c get a character
    %*[^'\n'] get the longest string possible that does not contain the character '\n'. The * specifies that we don't want to store this string.
    %*c get the next character (which is obviously the '\n'). The * specifies that we don't want to store thsi character.

    I haven't tested any of these code segments, but I'm pretty sure they're working. I suggest choosing whatever method makes the most sense to you.
    Callou collei we'll code the way
    Of prime numbers and pings!

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Look, I pretty sure that scanf doesn't scan in the '\n' char. You won't have to worry about that. It scans only what you specified into the specified vars (if string -- null terminated). fgets is the one that you have to do something like:
    Code:
    fgets(line, sizeof(line), stdin);
    line[strlen(line)-1] = '\0';
    To alter the '\n' to an early '\0'.
    1978 Silver Anniversary Corvette

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > line[strlen(line)-1] = '\0';
    If the user filled the buffer, there will be no \n to remove, and this instead overwrites the last char of user input.

    See several of my previous posts which use strchr

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Salem
    > line[strlen(line)-1] = '\0';
    If the user filled the buffer, there will be no \n to remove, and this instead overwrites the last char of user input.

    See several of my previous posts which use strchr
    Well, if you make a "comfortable" buffer (something like line[512] as the char array), you shouldn't have to worry about something like that. Taking the extra steps isn't worth it in this case and will only defeat the optimization purpose (not that that is what he/she is aiming at, but that should always be in the back of your mind).
    1978 Silver Anniversary Corvette

  9. #9
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Well, if you make a "comfortable" buffer (something like line[512] as the char array),
    So it's OK to use gets with a comfortably large buffer is it?

    All you're doing is moving the probability of a problem closer to 0 - that is all.

    > Taking the extra steps isn't worth it in this case and will only defeat the optimization purpose
    And what do you imagine is the difference between
    strlen( buff ) and strchr( buff, '\0' );

    A highly optimised bug is still a bug.

  10. #10
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Originally posted by Salem
    A highly optimised bug is still a bug.
    You're right, Salem. That is the best method to do it. Better off to be safe with looking towards the last char.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input statement problem
    By une in forum C Programming
    Replies: 3
    Last Post: 05-29-2007, 11:16 PM
  2. For loop problems, input please.
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-22-2007, 03:54 AM
  3. I would love some input on my BST tree.
    By StevenGarcia in forum C++ Programming
    Replies: 4
    Last Post: 01-15-2007, 01:22 AM
  4. Stupid Question
    By digdug4life in forum C++ Programming
    Replies: 22
    Last Post: 05-17-2005, 11:43 AM
  5. Help with Input Checking
    By Derek in forum C Programming
    Replies: 7
    Last Post: 06-17-2003, 03:07 AM