Reading input with scanf

This is a discussion on Reading input with scanf within the C Programming forums, part of the General Programming Boards category; I read in a 'int' using scanf, then a 'char' using scanf again, but the 2nd scanf reads the <enter> ...

  1. #1
    dat
    dat is offline
    Registered User
    Join Date
    Nov 2001
    Posts
    28

    Reading input with scanf

    I read in a 'int' using scanf, then a 'char' using scanf again, but the 2nd scanf reads the <enter> instead of the character that I type in next. When i add a \n in my control string like this:
    Code:
    scanf( "\n%c", &c );
    it doesn't read in the <enter>, can anyone explain to me why?
    Dat
    http://hoteden.com <-- not an porn site damnit!

  2. #2
    Registered User
    Join Date
    May 2003
    Posts
    4
    I quit using scanf() years ago. I always use gets() first
    and save input to a buffer, then use sscanf() to scan
    from the buffer. Instructors ought to teach this in
    first time/C courses! There's more important things
    to spend your time on.

    void myfunc(void)
    {
    char buffer[100], letter;

    gets(buffer);
    sscanf(buffer, "%c", &letter);
    }

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by bernmeister
    [B]I quit using scanf() years ago. I always use gets() first
    and save input to a buffer, then use sscanf() to scan
    from the buffer. Instructors ought to teach this in
    first time/C courses!
    Well now that's just stupid. Stupid of you, and stupid of your instructors. Want to watch me crash your program? All I have to do is input more information than your buffer and your program is now dead.

    Never use gets. Never teach gets.

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

  4. #4
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    I quit using scanf() years ago. I always use gets() first
    and save input to a buffer, then use sscanf() to scan
    from the buffer. Instructors ought to teach this in
    first time/C courses! There's more important things
    to spend your time on.
    Use fgets() instead. No buffer overruns.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading and processing input from keyboard
    By papagaio in forum C Programming
    Replies: 1
    Last Post: 11-12-2008, 03:59 PM
  2. Replies: 1
    Last Post: 07-10-2006, 07:30 AM
  3. Help with linked list (and reading user input)
    By p1kn1c in forum C Programming
    Replies: 2
    Last Post: 04-05-2006, 01:43 AM
  4. reading input file
    By dellebelle751 in forum C++ Programming
    Replies: 6
    Last Post: 09-28-2002, 03:05 PM
  5. reading input
    By alcoholic in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2001, 10:16 AM

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