Thread: problem with scanf function

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    1

    problem with scanf function

    Hi everyone. The problem I have looks very stupidly at the first sight and I am sorry for posting it. But even if I searched reference manuals and already posted questions in forums around the net, I wasn’t able to solve that.

    Could you anyone please tell me, why this isn’t working?

    Code:
      int nr,err;
      do {
       printf("\nenter a number (0-10):");
       err=scanf("%d",&nr);   
       if(err==1&&(nr<0||nr>10)) printf("the number must be between 0 and 10\n");   
      }while(err!=1||(err==1&&(nr<0||nr>10)));
    It works fine when you enter a good value, it works also when you enter wrong value, but digit. If you enter a character, it will start an unending repeating. Why doesn’t it ask to reenter the value?

    The only tips I’ve found was about flushall() function; I am not sure if it would be usable in my case too, but I can’t use that either – I have to do this in C not in C++.

    Thanks for any tips or links somewhere where relevant information could be found.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,334
    It doesn't ask because the part of the if statement isn't true -- if you type in "bob", the scanf is unable to process %d, so (a) "bob" is left in the input stream (this is why it just keeps repeating) and (b) err is set to 0. Since err is set to 0, your if doesn't trigger, but you do-while loop will loop. Since there's still stuff left in the input stream, scanf doesn't wait for more input but keeps trying to read what's there.

    You can read the FAQ about getting numbers for some ideas; there's also an FAQ about how to get rid of bad input typed in as well.

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by lida View Post
    Code:
      int nr,err;
      do {
       printf("\nenter a number (0-10):");
       err=scanf("&#37;d",&nr);   
       if(err==1&&(nr<0||nr>10)) printf("the number must be between 0 and 10\n");   
      }while(err!=1||(err==1&&(nr<0||nr>10)));
    Maybe you want err>=1 here, or >=0. Are you sure it "starts an unending repeating" but doesn't "ask for the value again"? What is it repeating then?

    edit: I think tabstop explains this.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-28-2009, 09:25 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 02:53 AM
  5. Replies: 5
    Last Post: 02-08-2003, 07:42 PM