Thread: Check input data

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    17

    Check input data

    In my programm I want to give as input a number from 0 to 100 and I need to deal with the situation in which a bad user of the program might give character(s) as input (while they shouldn't!).

    I've done this:

    Code:
    int num;
     printf("Give number of names:\n");
    int flag=scanf("%d",&num);
    
    while(flag!=1 || num<0 || num>100)
    {
        printf("Give number of names:\n");
       flag=scanf("%d",&num);
    }
    When I run this piece of ..... code and give characters for input, I get some great output:
    Give number of names:
    Give number of names:
    Give number of names:
    Give number of names:
    ....and keeps going forever....

    Anyone could share some ideas?
    Last edited by Cevris; 05-19-2011 at 07:50 AM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    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.

  3. #3
    Registered User
    Join Date
    May 2011
    Posts
    5
    i guess the character is never read, so it's still there. and the scanf is trying to read it but fails, so it keeps looping without getting an input from you.
    try to use a do while loop as commomTater suggested and add an if statement to check the flag:
    int num;
    int flag;
    do{
    printf("Give number of names:\n");
    flag=scanf("%d",&num);
    if(flag != 1)
    {
    char trash[10];
    scanf("%s",trash);
    continue;
    }
    }while(num<0 || num>100);
    return 0;
    Last edited by xiaohuai; 05-19-2011 at 10:08 AM.

  4. #4
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Cevris View Post
    In my programm I want to give as input a number from 0 to 100 and I need to deal with the situation in which a bad user of the program might give character(s) as input (while they shouldn't!).

    I've done this:

    Code:
    int num;
     printf("Give number of names:\n");
    int flag=scanf("%d",&num);
    
    while(flag!=1 || num<0 || num>100)
    {
        printf("Give number of names:\n");
       flag=scanf("%d",&num);
    }
    When I run this piece of ..... code and give characters for input, I get some great output:
    Give number of names:
    Give number of names:
    Give number of names:
    Give number of names:
    ....and keeps going forever....

    Anyone could share some ideas?
    Get rid of the first scanf() call entirely. Switch your loop to a do .. while construct with the test at the bottom... initialize num to 0 and flag to 0... and try it again.

  5. #5
    Registered User
    Join Date
    May 2010
    Posts
    76
    This works great but I know it's not desiredable:

    Code:
    #include <stdio.h>
    
    int main (void)
    {
    
    int num = 0;
    int flag = 0;
    
    do
    {
    	printf("Give number of names: ");
    	flag = scanf ("%d", &num);
    	getchar();
    	fflush (stdin);
    	}while (flag != 1 || num < 1 || num > 100);
    
    return 0;
    /
    Sorry, i'm just trying to get my head around using other methods instead of FFLUSH (STDIN); I decided after class I would try to figure this out which is what i'm doing, final exam tonight. In all my homework I used this, despite it being incorrect my professor never took issue to it.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Seriously fflush() is for output streams only... in your code it probably just causes a return from the call, and does nothing.
    Frankly, it should cause a run time exception since it is an obvious misuse of the function.

  7. #7
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by dolfaniss View Post
    Sorry, i'm just trying to get my head around using other methods instead of FFLUSH (STDIN); I decided after class I would try to figure this out which is what i'm doing, final exam tonight. In all my homework I used this, despite it being incorrect my professor never took issue to it.
    Please, don't go teaching people how to do things the wrong way. If you can't give proper advice, best not to give any. If you can't figure out how to properly flush the input buffer, you're probably a little too novice to be running around handing out any advice. And if your professor knew you were using fflush(stdin) and didn't bust you for it, he's not doing his job well, but I'm guessing he didn't read every line of every program of each of his students. It's wrong, stop using it in your code and stop teaching it. Read the tutorial Salem linked to in post #2, and ask us for help if you need to (new thread please).

  8. #8
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by xiaohuai View Post
    i guess the character is never read, so it's still there. and the scanf is trying to read it but fails, so it keeps looping without getting an input from you.
    try to use a do while loop as commomTater suggested and add an if statement to check the flag:
    Code:
     int num;
        int flag;
        do{
            printf("Give number of names:\n");
            flag=scanf("%d",&num);
            if(flag != 1)
            {
                char trash[10];
                scanf("%s",trash);
                continue;
            }
        }while(num<0 || num>100);
        return 0;
    I run this, but if I give characters for the first time it terminates. If I give a number greater than 100 or less than 0 at first place it runs correctly. Why is that?

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Code:
     int num;
        do{
             num = 0;
             printf("Give number of names (1 - 100) :  ");
             scanf(" %d",&num);                            
           }while(num<1 || num>100);

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    17
    Quote Originally Posted by CommonTater View Post
    Code:
     int num;
        do{
             num = 0;
             printf("Give number of names (1 - 100) :  ");
             scanf(" %d",&num);                            
           }while(num<1 || num>100);
    Nope....If you check it you will see that it runs forever.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok here you go...

    This is my exact test code, scoop and poop from my IDE (which is Pelles C, ver 6.50 rc 4)...
    Code:
    #include <stdio.h>
    
    
    int main (void)
    { int num = 0;
    
      do
       { num = 0;
         printf("Enter a number (1 - 100) : ");
         scanf("%d",&num); }
      while ((num < 1) || (num > 100));
    
      printf ("\nSuccessful entry = %d\n",num);
    
      return 0; }
    And here is a screen shot with invalid entries, blank lines, and finally a good entry...

    Attachment 10593

  12. #12
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Cevris View Post
    I run this, but if I give characters for the first time it terminates. If I give a number greater than 100 or less than 0 at first place it runs correctly. Why is that?
    Because who knows what value num has when you start? If you get to the end of the do-while with num=47, then the while condition is false and the loop terminates.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by tabstop View Post
    Because who knows what value num has when you start? If you get to the end of the do-while with num=47, then the while condition is false and the loop terminates.
    Ok... now I see the problem... he's getting a prompt that says "Enter a number" but he's typing FRED or something... yes that will make it run on.

    Interesting problem...

  14. #14
    Registered User
    Join Date
    May 2011
    Posts
    5
    try this: int num = -1;

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok this works... sort of... it runs on until it empties the input buffer... that is, if you enter FRED it will loop 4 times but stop at asking for the number...
    Code:
    #include <stdio.h>
    
    int main (void)
    { int num = 0;
    
      do
        { num = 0;
          printf("Enter a number (1 - 100) : ");
          scanf("%d",&num);
          getchar();
          printf("\n"); }
      while ((num < 1) || (num > 100)); 
    
      printf ("\nSuccessful entry = %d\n",num);
    
      return 0; }
    Frankly... I think this could be called a serious weakness in the library's scanf() function...


    Attachment 10597
    Last edited by CommonTater; 05-19-2011 at 11:35 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Adding data to existing data in an input file?
    By matthayzon89 in forum C Programming
    Replies: 4
    Last Post: 11-20-2010, 11:23 PM
  2. Check input!
    By johnybe in forum C Programming
    Replies: 2
    Last Post: 11-09-2009, 11:58 AM
  3. Check for input?
    By raterus in forum C Programming
    Replies: 5
    Last Post: 08-26-2009, 12:23 PM
  4. help with input check
    By negevy in forum C Programming
    Replies: 1
    Last Post: 09-02-2005, 07:14 AM
  5. check for bad input
    By tcs122499 in forum C++ Programming
    Replies: 9
    Last Post: 01-28-2003, 05:12 PM