Unable to enter additional input after validation

This is a discussion on Unable to enter additional input after validation within the C Programming forums, part of the General Programming Boards category; Originally Posted by RenFromPenn I just tried it and the text just keeps printing and printing. you should break out ...

  1. #16
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by RenFromPenn View Post
    I just tried it and the text just keeps printing and printing.
    you should break out of the loop after the user has done what you asked for
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  2. #17
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I don't even get a chance to enter more input. When i enter the incorrect information and press enter the text just starts scrolling across the screen. I have no opportunity to try entering the correct number a second time around.

  3. #18
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    602
    Again you have to flush the buffer, especially when using Visual C++. This is somewhat liken to having to call and extra getchar() after any user input.

  4. #19
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I'm not using Visual C++. I am writing in C. I am just using this complier to write the program in. Could you please just tell me how to flush it? I tried and it didn't work. I have googled it and I can't find the information.

  5. #20
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    Okay, I got it to work. I broke the while loop by adding flushall();

    Thank you all for your help!

  6. #21
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by RenFromPenn View Post
    Okay, I got it to work. I broke the while loop by adding flushall();

    Thank you all for your help!
    That is the wrong solution - flushall is a function that isn't guaranteed to do anything useful or meaningfull for input channels. _DO_ look up the FAQ link that was posted a few posts back. It contains a loop that does getchar(), which is safe and correct.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #22
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I did look at the FAQs, but I didn't understand how to implement the flush. Would someone please just paste it into my code and help me with this?

  8. #23
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
      int   ch;
    
      while ((ch = getchar()) != '\n' && ch != EOF);
    put int ch in with your variables, and the line starting with "while" where you want to clean up your input buffer. Calling this after any scanf should be fine (assuming you don't expect someone to enter a bunch of stuff on one line, but you read it using several scanf stataments, e.g a loop to read 10 integers, and someone expects to be able to enter the 10 numbers in a line).

    You could also put all of the above code into a function, and then call that function when you feel a need to clean up the input.

    The above function is ALSO very useful if you are mixing scanf() and fgets() or scanf with "%s" or "%c", since scanf leaves newlines in the input buffer, and when you then call fgets, it sees the newline from your the input to scanf, and takes the input to be blank.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Help With a BlackJack Program in C
    By Jp2009 in forum C Programming
    Replies: 15
    Last Post: 03-30-2009, 10:06 AM
  2. input validation, kinda
    By dynamethod in forum C++ Programming
    Replies: 1
    Last Post: 09-15-2008, 12:52 AM
  3. C++ array input validation.
    By c++baby in forum C++ Programming
    Replies: 2
    Last Post: 03-23-2008, 04:39 AM
  4. Input validation
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-02-2001, 10:18 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 06:30 AM

Tags for this Thread


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