Thread: what does this function do ???

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    8

    what does this function do ???

    Can someone please help me figure out what this function does ?
    It's suposed to det an integer number between the "temp" value yet when I run the program and I give in a value it just keeps on looping... (Id'like to post the whole program... can i ? It's the infamous Game of Life...)

    Thanks

    Code:
    int Get_int_in_range(int min, int max) {
        int temp;
    
        scanf("%d", &temp);
        while ( (temp < min) || (temp > max) ) {
            printf("Please enter a number >= %d and <= %d\n", min, max);
            scanf("%d", &temp);
        } 
        return temp;
    }

  2. #2
    ~viaxd() viaxd's Avatar
    Join Date
    Aug 2003
    Posts
    246
    Code:
    while ( (temp > min) && (temp < max) )

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    127
    What input are you typing in? If it's not a digit then scanf will fail, leave the invalid character in the stream, and then proceed to fail on it infinitely. You need to clear any error conditions and then remove the guilty character from the stream before trying again.
    Code:
    int Get_int_in_range(int min, int max) {
      int temp;
    
      while ( scanf("%d", &temp) != 1 || (temp < min) || (temp > max) ) {
        clearerr(stdin);
        while ( (temp = getchar()) != EOF && temp != '\n' ) {
        }
        printf("Please enter a number >= %d and <= %d\n", min, max);
      }
    
      return temp;
    }

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    8
    oh...

    Thanks a lot !!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  2. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  3. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  4. Replies: 28
    Last Post: 07-16-2006, 11:35 PM
  5. const at the end of a sub routine?
    By Kleid-0 in forum C++ Programming
    Replies: 14
    Last Post: 10-23-2005, 06:44 PM