So i am working on a linked list program and am using functions like insert, delete and search which require taking input from the user and so i tried using GetInt() from the CS50 library for the same but everytime anyone of the above functions is being executed, GetInt() is directly providing Retry: prompt even before i am giving an input. I have not been able to find out why this is happening and eventually i replaced it with scanf but i am still curious to know why GetInt() behaved like that.
I am adding the code snippets and screen shots for better understanding of my problem.
The following is the search function where i am using the GetInt() function.

   void search(void)
       // prompt user for number
       printf("Number to search for: \n");

       // get list's first node
       node* ptr = first;
       // search for number
      while (ptr != NULL)
          if (ptr->num == n)
              printf("\nFound %i!\n", n);
          ptr = ptr -> next;
      printf("\nProvided number is not present \n");

Attached below is the screenshot of the terminal where i have run my program, i have highlighted my problem in yellow text.

Terminal Screenshot

I have earlier added 4 elements into my linked list and when i am trying to search for any number the prompt is always followed by Retry prompt. So if anyone could explain why this might be happening it would be a great help.

1 Answer 1


The only reason that GetInt() would return "Retry" is if it recieved a non-integer from standard input (ie, from your keyboard), as the definition says:

 * Reads a line of text from standard input and returns it as an
 * int in the range of [-2^31 + 1, 2^31 - 2], if possible; if text
 * does not represent such an int, user is prompted to retry.  Leading
 * and trailing whitespace is ignored.  For simplicity, overflow is not
 * detected.  If line can't be read, returns INT_MAX.

int GetInt(void)
    // try to get an int from user
    while (true)
        // get line of text, returning INT_MAX on failure
        string line = GetString();
        if (line == NULL)
            return INT_MAX;

        // return an int if only an int (possibly with
        // leading and/or trailing whitespace) was provided
        int n; char c;
        if (sscanf(line, " %d %c", &n, &c) == 1)
            return n;
            printf("Retry: ");

I've used GetInt() a lot and have never seen it return 'Retry' without some keyboard entry, even only a carriage return. As you can see, the function will not return unless it receives a valid value from standard input.

You might try running your program on http://run.cs50.net to eliminate any appliance problem.

  • I live with Monkey. I am facing the same problem with his code on my laptop. Jul 6, 2014 at 15:34
  • Indications point to a spare newline sitting in the stdin buffer at the time that GetInt() is called by your search function. I'd look at whatever runs before your search is called. While you are running the program, do you ever hit 'return'? Are you sure that that newline char is then used and cleared out of stdin?
    – curiouskiwi
    Jul 6, 2014 at 19:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .