If statement is falling through. Not waiting for second user input

This is a discussion on If statement is falling through. Not waiting for second user input within the C Programming forums, part of the General Programming Boards category; Does anyone know why this is falling through? the second printf statement displays however, program terminates without waiting for input ...

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    141

    If statement is falling through. Not waiting for second user input

    Does anyone know why this is falling through?
    the second printf statement displays however, program terminates without waiting for input character from user. I've tried fflush(stdin) but with no luck

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char ch;
    	int n;
    	
    	printf("user data: ");
    	scanf("%d",&n);
    	
    	if(n==5)
    	{
    		/*obtain more data from user*/	
    		printf("Enter more Data? ");
    		scanf("%c",&ch); 
    	}
    
    return(0);
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    When the user enters the number, the newline is left in the input buffer. It is then read when you use %c with the next scanf. An appropriate solution is to read and discard that newline character immediately after reading the number.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jan 2011
    Posts
    141
    thanks for the quick reply.
    I've tried to clean out the buffer by fflush(stdin), however, the problem still persists.
    Another command I tried was flushall() but im getting an error

    Code:
    #include <stdio.h>
    
    int main()
    {
    	char ch;
    	int n;
    	
    	printf("user data: ");
    	scanf("%d",&n);fflush(stdin);  /*also tried flushall()*/
    	
    
    	if(n==5)
    	{
    		/*obtain more data from user*/	
    		printf("Enter more Data? ");fflush(stdin); 
    		ch=getchar(); 
    	}
    
    
    return(0);
    }

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,409
    Quote Originally Posted by bos1234
    I've tried to clean out the buffer by fflush(stdin), however, the problem still persists.
    fflush(stdin) results in undefined behaviour. I would expect something like this:
    Code:
    #include <stdio.h>
    
    int main()
    {
        int ch;
        int n;
    
        printf("user data: ");
        scanf("%d", &n);
        while ((ch = getchar()) != '\n' && ch != EOF);
    
        if (n == 5)
        {
            /* obtain more data from user */
            printf("Enter more Data? ");
            ch = getchar();
            printf("You entered: %c\n", ch);
        }
    
        return 0;
    }
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do you read input from the user without waiting?
    By Isaiah in forum C Programming
    Replies: 5
    Last Post: 01-23-2012, 02:53 PM
  2. Help, scanf isn't waiting for user input
    By Blasz in forum C Programming
    Replies: 5
    Last Post: 05-07-2010, 05:32 AM
  3. Getting input without waiting for keypress
    By Anon48 in forum C Programming
    Replies: 2
    Last Post: 04-08-2005, 02:18 AM
  4. Getting input without waiting for keypress
    By Ink in forum C Programming
    Replies: 4
    Last Post: 10-01-2004, 01:38 AM
  5. Waiting for input
    By Jareds411 in forum Windows Programming
    Replies: 5
    Last Post: 09-16-2004, 06:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21