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");
//EVEN IF NEWLINE CHARACTER IS REMOVED THE PROBLEM REMAINS
int n = GetInt(); //THIS IS CAUSING THE PROBLEM IN MY OPINION
// get list's first node
node* ptr = first;
// search for number
while (ptr != NULL)
{
if (ptr->num == n)
{
printf("\nFound %i!\n", n);
sleep(1);
break;
}
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.
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.