Thread: user input

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    16

    user input

    when asking the user for input is there any way that you can enable the user to go back and alter there input before they press enter

  2. #2
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    yes. Using getchar() and gets() make you press enter before it sends the information, and will allow you to go back and change what you type.

  3. #3
    Registered User red_baron's Avatar
    Join Date
    May 2002
    Posts
    274
    do you mean going back with the arrow keys or erasing everythign?
    ¿Red Baron?

    "Imagination is more important than knowledge"
    -Albert Einstein (1879-1955)

    Check out my games!

    [code] /* dont forget code tags! */ [/code]

  4. #4
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    like being able to use the backspace key.

  5. #5
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Draco
    yes. Using getchar() and gets() <snip>
    Best not use gets(), it's not good! It has no bounds checking and you'll end up in trouble. Use fgets() instead..
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  6. #6
    Registered User Draco's Avatar
    Join Date
    Apr 2002
    Posts
    463
    ok, ok....my book doesn't cover fgets(). What's the difference by the way?

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >What's the difference by the way?
    fgets is more generic, safer, and it makes you feel all warm and fuzzy. Whereas the call to gets looks like this:

    /* No size is given, very unsafe */
    gets ( array );

    fgets looks more like this:

    /* A size is given, and you can read from files too! */
    fgets ( array, sizeof array, stdin );

    Note that gets was a part of the original portable I/O library in the early days of C. It should have been removed with the rest of the trash in the C89 ANSI/ISO standard, but it wasn't for unknown reasons.

    -Prelude
    My best code is written with the delete key.

  8. #8
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Exclamation Fgets?

    hmm.... fgets is just like a file structure. however, that situation isn't using any fgets ...

  9. #9
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Lightbulb Why not use ...

    Why not use
    : scanf ("%30[^\n]", &array);

    well... the system could control the input if char more that the limit. the next 29 char (not include '\0') will not scan it.

  10. #10
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Why not use
    >: scanf ("%30[^\n]", &array);
    scanf was designed for formatted input, not line input, and most certainly not user input. So why take advantage of it's features to create a nasty and dangerous hack when there's a function much better suited to this task?

    -Prelude
    My best code is written with the delete key.

  11. #11
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291

    Arrow RE :

    Yup, i guess you are right.

    however if using ...
    fgets ( array, sizeof array, stdin );

    we din't know how long the string of the input could be?
    or we can try to give a warning msg that the input is too long and tell the user to input again.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >we din't know how long the string of the input could be?
    I assume you mean that the input could theoretically be longer than the array can hold? In such a case where you suspect that to happen you can read the size of the buffer (fgets will stop at the delimiting size) and then test to see if there is a newline in the buffer. If there isn't then there is still more input to read.

    This can be handled many ways, one of which would be an intermediate buffer which reads from input and appends its contents to a larger buffer until the newline is reached. The larger buffer would almost always be dynamically resized accordingly.

    -Prelude
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Jun 2002
    Posts
    9
    Originally posted by Prelude
    >Why not use
    >: scanf ("%30[^\n]", &array);
    scanf was designed for formatted input, not line input, and most certainly not user input. So why take advantage of it's features to create a nasty and dangerous hack when there's a function much better suited to this task?

    -Prelude
    Is using scanf in this way dangerous? I mean will it lead to some buffer overflow or somethin' like that?

  14. #14
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    oh ok ... thank guy

  15. #15
    ~- Y u n a -~ beely's Avatar
    Join Date
    Dec 2001
    Posts
    291
    nop ... it'll scan until 29 char including the \0 char. so the next 29 char that user inputed will not save in the array variable.

    well... it's could control the user not to enter more than 29 char.

    but you should set the warning msg to user to give them know there's an error on input data

    :[^\n] > will scan until the '\n' char

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 03:53 PM
  3. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM
  4. Getting user input for filename loop
    By jpchand in forum C++ Programming
    Replies: 1
    Last Post: 09-16-2003, 06:37 AM
  5. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM