Thread: Accepting formatted input

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Password:
    Join Date
    Dec 2009
    Location
    NC
    Posts
    587
    After using scanf(), the "\n" is left in stdin, so getchar() does nothing. That's the cause of your infinite loop, I think.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by User Name: View Post
    After using scanf(), the "\n" is left in stdin, so getchar() does nothing. That's the cause of your infinite loop, I think.
    If 4 valid entries aren't entered it goes into the else loop which should clear the entire input buffer since it loops around until it reaches a '\n' so I don't see how '\n' would be getting left in the system and causing problems.

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by fxtdr79 View Post
    If 4 valid entries aren't entered it goes into the else loop which should clear the entire input buffer since it loops around until it reaches a '\n' so I don't see how '\n' would be getting left in the system and causing problems.
    the problem is - you do not know if scanf left something in the input buffer...

    I would think the fgets/sscanf version should be more stable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    35
    Quote Originally Posted by vart View Post
    the problem is - you do not know if scanf left something in the input buffer...

    I would think the fgets/sscanf version should be more stable
    Do you know how to correct eh fgets/sscanf code I put in the top post to get it work correctly ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Input class project (again)
    By Elysia in forum C++ Programming
    Replies: 41
    Last Post: 02-13-2009, 10:52 AM
  2. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  3. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  4. Problems reading formatted input with sscanf
    By Nazgulled in forum C Programming
    Replies: 17
    Last Post: 05-10-2006, 12:46 AM
  5. accepting input while writing to screen
    By variable in forum C Programming
    Replies: 17
    Last Post: 02-06-2005, 10:14 PM