Thread: Program doesn't wait for user input!

  1. #1
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59

    Program doesn't wait for user input!

    Hi, I am trying to make a program that finds the Nth node in the linked list where N is user input. When the user adds the element in the linked list, after each addition I ask whether to continue addition or stop. Here the user is expected to enter Y/N as his choice but the program skips this portion and doesn't wait for user input here.

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct node 
    {
    	int nodeValue;
    	struct node *link;
    };
    
    struct node *addElement(struct node *headPtr,int elem); 
    int lengthList(struct node *headPtr);
    int Find_Nth_Node(struct node *headPtr,int number);
    
    int main()
    {
    	int num,temp,value;
    	char choice;
    	struct node *front=NULL;
    	
    	/*printf("Enter number of nodes:");
    	scanf("%d",&num);*/
            choice='y';
    	printf("\nEnter elements:\n");
    	while(choice=='y'||choice=='Y')
    	{
    		scanf("%d",&temp);
    		front=addElement(front,temp);
    		printf("\nWant to enter more?");
    		scanf("%c",&choice);         //doesn't wait for user input
    	}
    
    	printf("\nEnter the number of node whose value is desired:");
    	scanf("%d",&num);
    	if(num>lengthList(front))
    	{
    		printf("\nSorry, there are less than %d nodes in the list",num);
    		return 0;
    	}
    	value=Find_Nth_Node(front,num);
    
    	printf("\nThe value at node %d is:%d",num,value);
    	getchar();
    	return 0;
    }
    
    struct node *addElement(struct node *headPtr,int elem)
    {
    	struct node *trav_ptr,*temp;
    	temp=(struct node *)malloc(sizeof(struct node));
    	if(temp==NULL)
    	{
    		printf("\nError, no free space");
    		return 0;
    	}
    	else
    	{
    		temp->nodeValue=elem;
    		temp->link=NULL;
    	}
         
    if(headPtr==NULL)											       	                                    //If linked list is empty
    		headPtr=temp;
    	else																		//If linked list is not empty
    	{
    		trav_ptr=headPtr;
    		while(trav_ptr->link!=NULL)
    			trav_ptr=trav_ptr->link;
    		trav_ptr->link=temp;
    	}
    	return headPtr;
    }
    
    int lengthList(struct node *headPtr)
    {
    	int length=1;
    	while(headPtr->link!=NULL)
    	{
    		headPtr=headPtr->link;
    		length++;
    	}
    	return length;
    }
    
    int Find_Nth_Node(struct node *headPtr,int number)
    {
    	int ctr=0;
    	while(ctr++<number)
    		headPtr=headPtr->link;
    	return headPtr->nodeValue;
    }
    Waiting for some suggestions.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    All the basic format conversions (except %c) skip white space and newlines.
    %c is different, it unconditionally reads the next character.

    In your code, this will typically be the \n left at the end of the previous %d.

    You could read this -> Cprogramming.com FAQ > Flush the input buffer

    Or you could use fgets() to read ALL input into a buffer, then use whatever (perhaps sscanf) to get from the buffer whatever interests you.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Thanks for the suggestion, you are right, the choice variable was taking newline as the input and continuing the execution without user input. So i flushed the input buffer and now the program works as intended.

  4. #4
    Registered User
    Join Date
    Jul 2011
    Posts
    11
    I see, so the %c will actually read in the "Enter" key from the %d above?

  5. #5
    Registered User
    Join Date
    Dec 2010
    Location
    Delhi, India
    Posts
    59
    Yes, I debugged the program and choice variable contained ASCII 10(Linefeed) that caused the program to produce mysterious results.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by gaurav_13191 View Post
    Thanks for the suggestion, you are right, the choice variable was taking newline as the input and continuing the execution without user input. So i flushed the input buffer and now the program works as intended.

    Quick and easy fix...
    Code:
    scanf(" %c",&ch);
    Note the space inserted between the " and the %.

  7. #7
    Registered User
    Join Date
    Jul 2011
    Posts
    11
    Quote Originally Posted by CommonTater View Post
    Quick and easy fix...
    Code:
    scanf(" %c",&ch);
    Note the space inserted between the " and the %.
    I almost forgot about this method,
    every time I encounter that problem I just use fflush(stdin) .

    any difference by using fflush or add blackspace behind it?

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Blane View Post
    I almost forgot about this method,
    every time I encounter that problem I just use fflush(stdin) .

    any difference by using fflush or add blackspace behind it?
    Unless you're using that braindead old Turbo C or Turbo C++ compiler fflush(stdin) is some serious bad juju... that can cause data loss on buffered input streams.

  9. #9
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    Quote Originally Posted by Blane View Post
    any difference by using fflush or add blackspace behind it?
    After an exhaustive internet search, making multiple phone calls, and then writing a few letters; I was rewarded with this little gem hidden away by the all powerful mods to ensure us mere mortals would never find it:

    FAQ-fflush(stdin)

    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 10-12-2010, 01:40 PM
  2. getchar() doesn't wait for input
    By cantinero74 in forum C Programming
    Replies: 5
    Last Post: 04-27-2010, 09:46 AM
  3. How do I make it wait for user input
    By earth_angel in forum Windows Programming
    Replies: 1
    Last Post: 06-29-2005, 01:34 PM
  4. user input and program design
    By Chaplin27 in forum C++ Programming
    Replies: 2
    Last Post: 06-09-2005, 08:53 AM
  5. Why won't this wait for user input?
    By incognito in forum C Programming
    Replies: 9
    Last Post: 03-05-2003, 05:36 AM