Thread: user input

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

    oops!

    ah one more ...

    the next 29 char will not lead to another "scanf" ... so it'd be safer.

    for more info : fflush (stdin) -could cleanup all the input data that have been entered to prevent from the next char input.

    reason : '\n' is char - so scanf will detect the char and then detect again ... <sorry language not really good enough>

    that mean you can't enter the data on the next char variable (variable >> input2)

    input2 = '\n'

    here is your data on the input2

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Is using scanf in this way dangerous?
    What happens if the user enters 30 characters before a newline? The array is filled with characters and there's no room for a nul terminator. Since scanf doesn't place the \0 after a read, this can and should cause problems with functions that rely on the nul being there.

    >for more info : fflush (stdin) -could cleanup all the input data
    >that have been entered to prevent from the next char input.
    No, fflush is not defined for input streams, so fflush ( stdin ) invokes undefined behavior. To clear the stream after a call to scanf, a loop would be best:

    while ( getchar() != '\n' );

    or for more thorough checking:
    Code:
    void ifflush ( FILE *fp )
    {
      int c;
      while ( ( c = getc ( fp ) ) != EOF && c != '\n' );
    }
    >that mean you can't enter the data on the next char variable
    Correct, calls to scanf tend to leave the newline in the stream buffer. This causes problems when you try to perform another read. Yet another reason to avoid scanf when possible.

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

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