Input Tolerance

This is a discussion on Input Tolerance within the C Programming forums, part of the General Programming Boards category; Hi guys, Here's a little bit of my code I'd like help with (I can't post it all as it's ...

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    Input Tolerance

    Hi guys,

    Here's a little bit of my code I'd like help with (I can't post it all as it's an assignment, but I've completed this portion for you to test if necessary):

    Code:
    #include <stdio.h>
    
    int main (void)
    { int n;
    
    do {
             
                printf ("\nHow many resistors would you like to enter?\n  [2 to 10 Resistors can be checked]\n\n");
             
                scanf ("%d", &n);
             
                if (n < 2 || n > 10)
                   { printf ("\nYou have entered a number out of range.\nPlease re-enter!\n"); }           
                
                }   while (n < 2 || n > 10);
                    
                    printf ("\nNumber of resistors to test: %d\n", n);
    system ("PAUSE");
    return (0);
    }
    I'd like to implement the following code into this part of my program, such that if the user does not input a digit, or inputs a character, then the program will output an error message "Input not accepted".

    I don't want it to display the bit where it says "one converted as..", if chk does equal 1 then I want it to continue the program as normal.

    Code:
    #include <stdio.h>
    
    
    int main (void)
    {   int x, chk;
    
    
        printf ("\n Input one decimal\n");
        chk = scanf ("%d", &x);
        
        if (chk == 1)
           printf ("\n one converted as %d\n",x);
        else
            printf ("\nInput not accepted");
            
    
    
    system ("PAUSE");
    return (0);
    }
    No matter how I try to implement it, everytime I hit a character it'll just initiate an endless loop.

    How can I do it? Or can you not do it inside a do while loop?

    Thanks in advance!

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    on line 10 in your second sample add..
    Code:
    while (getchar() != '\n');
    It goes into an infinite loop because scanf() is not purging the characters out of the input buffer and keeps re-reading the same thing over and over again.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Cheers!

    Run into a slight problem here;

    Code:
    do {
             
                printf ("\nHow many resistors would you like to enter?\n  [2 to 10 Resistors can be checked]\n\n");
                
                gets(vld);
                chk = sscanf (vld, "%d", &n);
                
                if (chk != 1)
                   { printf ("\nERROR; You have entered a character.  Please Enter a number!\n"); }
                   else
                   if (n < 2 || n > 10)
                   { printf ("\nYou have entered a number out of range.\nPlease re-enter!\n"); }  
                
                }   while (n < 2 || n > 10);
    I've used this method to the other two scanf requests further down my program and work fine, but this one is being silly....everytime I enter a letter it'll come out with the error but also just input 2 into the number of resistors, and not ask the user to re-enter again...

    can anyone help?

    Thanks!

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    forgot to add

    Code:
    for (i = 0; i < n; i++)
    before the loop that fixed it...


    BUT if i enter a letter, say "m"..it'll request the user to re-enter as desired...however if the user enters "m" again, we're back to the initial problem I posted (it'll input 2 and exit loop by itself)...

    If i enter "m" then enter "1" (or a number out of range), it'll ask the user to re-enter again...if I put a letter in and get the error desired it'll request the user to re-enter.....this way it'll work indefinitely like i want it to.

    why is it not working like in the first scenario...
    Last edited by nelly26; 12-11-2011 at 02:36 PM.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Ok, just retested...

    if I enter 3....it'll keep asking me to re-enter until i've re-entered "3" three times.
    it's doing it with any input number .

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by nelly26 View Post
    Cheers!

    Run into a slight problem here;

    Code:
    do {
             
                printf ("\nHow many resistors would you like to enter?\n  [2 to 10 Resistors can be checked]\n\n");
                
                gets(vld);
                chk = sscanf (vld, "%d", &n);
                
                if (chk != 1)
                   { printf ("\nERROR; You have entered a character.  Please Enter a number!\n"); }
                   else
                   if (n < 2 || n > 10)
                   { printf ("\nYou have entered a number out of range.\nPlease re-enter!\n"); }  
                
                }   while (n < 2 || n > 10);
    I've used this method to the other two scanf requests further down my program and work fine, but this one is being silly....everytime I enter a letter it'll come out with the error but also just input 2 into the number of resistors, and not ask the user to re-enter again...

    can anyone help?

    Thanks!
    Delete line 5 ... and stop using gets altogether. There's all kinds of reasons not to use it and not one good one why you need it.
    Add...
    Code:
    while(getchar() != '\n');
    Right after your scanf().

    Also lose the for() loops, that's why it's repeating.

    Your entry loops should look like this....
    Code:
    do
       printf("This is to prompt the user : ");
    while( (scanf("%d", &variable) < 1) || (variable < minvalue) || (variable > maxvalue));

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    But your method doesn't allow me to return an error message to the user...

    and the gets works perfectly fine in other instances in my application which also have very similar loops, accept they have the for loop and this bit doesn't
    Last edited by nelly26; 12-12-2011 at 02:24 AM.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Nelly, It's seldom desirable to send "error messages", in fact it can and does annoy the heck out of the user who will stop reading them after a while and complain that the screen is all messed up...

    The same philosophy can be applied to other annoying things like constantly clearing the screen or things that flash or change colours. These things may look cool as all git out, but they actually do little more than break an operator's concentration, slowing them down and inhibiting their productivity on the job. Just think about an operator hired to "cold key" (raw data entry) a 100,000 item inventory listing into an initial data base... it's going to take about a month to begin with... more than twice as long if things are bouncing around in front of them, confusing them and slowing them down....

    It is seldom necessary to stop everything, chastize the user, then resume...

    The problem with gets is that it will allow you to enter too much stuff and because it has no precautions built in will actually overwrite memory outside of an array's bounds... If you have an entry array of 20 characters and the user keys in 255... gets() will quite happily write 255 characters to memory, which will crash most programs.

    It's not whether it works when everything is going as it should that's the problem. It's what happens when things don't go as they should... Many of windows "security problems" can be directly attributed to the use of gets() in older programs...

    There is a more dependable replacement. fgets() includes a buffer size limit that won't let an operator overrun the allocated buffers for data entry... fgets(&value,size,stdin) will not try to stuff more than size characters into a buffer... look it up in your compiler's library documentation.
    Last edited by CommonTater; 12-12-2011 at 10:36 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 10-29-2011, 07:02 PM
  2. Replies: 1
    Last Post: 09-24-2009, 01:28 AM
  3. Printing Length of Input and the Limited Input
    By dnguyen1022 in forum C Programming
    Replies: 33
    Last Post: 11-29-2008, 03:13 PM
  4. GetPixel tolerance
    By JordanCason in forum C++ Programming
    Replies: 8
    Last Post: 12-06-2007, 10:47 PM
  5. While loop and tolerance
    By MethodMan in forum C Programming
    Replies: 17
    Last Post: 10-06-2004, 08:29 AM

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