Thread: Unable to enter additional input after validation

  1. #1
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16

    Unable to enter additional input after validation

    Hi,

    I am trying to write a program that asks the user to enter a number between 0 and 9. I also have to validate the input. That part works just fine. The problem comes in when they enter something other than 0-9. The error message displays and the prompt appears again which asks for them to enter a number between 0-9. So, the validation works like a charm. For whatever reason, however, when I tested the program I couldn't type anything at this point to input something else. Any ideas what is causing that?

    Here is the code:

    Code:
    #include <stdafx.h>
    
    int main ()
    {
    
    	 int user_input;
    
    printf("Please enter a number between 0 and 9: ");
    scanf("%d", &user_input);
    
    if (user_input < 0 || user_input > 9)
    
    {
            printf("Error: That is not a valid entry!\n");
    		printf("Please enter a number between 0 and 9: ");
    		scanf("%d", &user_input);
    }
    
    else printf("Thank you! You selected %d\n", user_input);
      
      return 0;
    }

  2. #2
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    Have you thought of using a while loop to check for the proper condition?

  3. #3
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    Yeah, I tried a while loop, but when I entered incorrect information to test validation the text "Error: That is not a valid entry! Please enter a number between 0 and 9:" just kept scrolling across my screen.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    scanf doesn't clean the input if you make a mistake.

    Which is why you should use fgets() to read a line, then sscanf to parse it (perhaps).
    If you reject the line at the parsing stage, you just go round and read another line.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Flush your input.

  6. #6
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    How do I do that? I'm completely new to this, so please forgive uninformed questions.

  7. #7
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    oh and why the use of the pre compiled header include statement? Why not use stdio.h?

  8. #8
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    For what ever reason I can not duplicate your scenario. What compiler are you using. With the simple change from your if to the a while statement and removing the else statement should do the trick for you:

    Code:
    #include <stdio.h>           /* !note this change!*/
    
    int main ()
    {
    
             int user_input;
    
    printf("Please enter a number between 0 and 9: ");
    scanf("%d", &user_input);
    
    while (user_input < 0 || user_input > 9)
    
    {
            printf("Error: That is not a valid entry!\n");
                    printf("Please enter a number between 0 and 9: ");
                    scanf("%d", &user_input);
    }
    
    printf("Thank you! You selected %d\n", user_input);
    
      return 0;
    }

  9. #9
    Registered User slingerland3g's Avatar
    Join Date
    Jan 2008
    Location
    Seattle
    Posts
    603
    If the user does not input a valid number then there will be an issue. Thus flushing may overcome that problem.

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by RenFromPenn View Post
    How do I do that? I'm completely new to this, so please forgive uninformed questions.
    There is FAQ on this site

    FAQ > How do I... (Level 2) > Flush the input buffer
    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

  11. #11
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I am using Microsoft Visual C++ to write my C program. When I try to use stdio.h it generates an error message and won't let me proceed.

    I tried entering the code just as you provided it and the same error appeared,: fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source:

    When I replaced it with stdafx.h I experienced the same loop that I mentioned previously.

  12. #12
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    Okay, I read the FAQs page and I changed the program to this:

    Code:
    #include <stdafx.h>
    
    int main ()
    {
    
    	 int user_input;
    
    	 puts;
    
    printf("Please enter a number between 0 and 9: ");
    scanf("%d", &user_input);
    
    if (user_input < 0 || user_input > 9)
    
    {
            printf("Error: That is not a valid entry!\n");
    		printf("Please enter a number between 0 and 9: ");
    		scanf("%d", &user_input);
    }
    
    else printf("Thank you! You selected %d\n", user_input);
      
      return 0;
    }
    That still didn't allow me to enter another number if I initially entered incorect input.

  13. #13
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    1. you should use loop
    2. you should check the return value of scanf
    3. you should clear the input stream

    if you want to discard the stdafx.h - disable the precompiled headers in the project settings
    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

  14. #14
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    Quote Originally Posted by vart View Post
    1. you should use loop
    2. you should check the return value of scanf
    3. you should clear the input stream

    if you want to discard the stdafx.h - disable the precompiled headers in the project settings
    Well, okay, maybe I should do all of that, but I don't know how. The loop doesn't wrok as it results in an infinate loop. I thought my program did check the value of scanf. I tried clearing the input stream, but puts didn't do anything but create an error.

    **Update**

    I disabled the precompiled headers as suggested. I'm going to run the while loop now to see if it works.
    Last edited by deleted_user; 11-20-2008 at 04:31 PM.

  15. #15
    Deleted Account
    Join Date
    Nov 2008
    Posts
    16
    I just tried it and the text just keeps printing and printing.

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, 11:18 PM
  5. terminate 0 - PLEASE HELP
    By Unregistered in forum C Programming
    Replies: 11
    Last Post: 11-21-2001, 07:30 AM

Tags for this Thread