Thread: something strange

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    9

    something strange

    it enters to endless loop
    it doesnt stop for scanf it leaves the old values and enters to an endless loop.
    can anybody explain to me please what am i doing wrong????

    Code:
    while(numH>=20||numW>=20||numP>=10||numH<=0||numW<=0||numP<=0){
    		printf("Incorrect input ,try again\n");
    		printf("\nEnter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
    		scanf("%d %d %d",&numH,&numW,&numP);
    	}

  2. #2
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The code works but I think not as you expect it to work.
    You have to enter all three values separated by a blank.
    if you want to enter them on by one you have to use three scanf statements, or use the this format-string
    Code:
    "%d\n%d\n%d"
    Kurt

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by ZuK
    or use the this format-string
    Code:
    "%d\n%d\n%d"
    http://www.eskimo.com/~scs/C-faq/q12.17.html
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  4. #4
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    I read that faq-entry but I do not understand it.(guess english is more complicated than I thought )
    I tried my format-string and it does what I expect. It behaves strange when I put \n als last char in the format-string.
    Kurt
    Last edited by ZuK; 09-03-2005 at 02:07 PM.

  5. #5
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    it doesnt stop for scanf it leaves the old values and enters to an endless loop
    That's because you don't clear the stream of any errors and then discard the bad input. Naturally scanf() will fail again on the same bad input if you don't get rid of it first:
    Code:
    while(numH>=20||numW>=20||numP>=10||numH<=0||numW<=0||numP<=0){
      clearerr(stdin);
      while ((c = getchar()) != EOF && c != '\n')
        ;
      printf("Incorrect input ,try again\n");
      printf("\nEnter puzzle height( < 20 ) width( < 20 ) number of pieces( <= 10 )\n");
      scanf("%d %d %d",&numH,&numW,&numP);
    }
    It behaves strange when I put \n als last char in the format-string.
    So don't do that. A lot of people get confused with how scanf() deals with whitespace in the format string.
    Just because I don't care doesn't mean I don't understand.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,664
    I'm pretty sure examples of how to use fgets and sscanf are in the local FAQ...
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strange error -- COM DLL is not installed correctly?
    By George2 in forum C# Programming
    Replies: 0
    Last Post: 07-16-2007, 08:32 AM
  2. strange linking error -- can not find shared library
    By George2 in forum C Programming
    Replies: 2
    Last Post: 07-10-2006, 10:51 PM
  3. Strange results using dnsapi and windns
    By Niara in forum Networking/Device Communication
    Replies: 3
    Last Post: 08-13-2005, 10:21 AM
  4. Strange response from MSVC
    By VirtualAce in forum Game Programming
    Replies: 2
    Last Post: 04-17-2004, 07:40 AM
  5. bcc32 compiling error (really strange!!)
    By jester in forum C++ Programming
    Replies: 14
    Last Post: 01-26-2002, 04:00 PM