Thread: fgetchar() doesnt work

  1. #1
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305

    fgetchar() doesnt work

    I have this program to see all the char input and output functions. It works fine except at the fgetchar() line. The problem is that at that point it should wait for me to enter the character before it moves ahead but it simply seems to iterate through that without me even entering anything to the next line.

    [insert]
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(void)
    {
    	char ch;
    
    	printf("\n Press any key to continue");
    	getch(); // will not echo the char
    	//printf("\n %c", ch);
    
    	printf("\n Type any character");
    	ch = getche(); // will echo the char
    	//printf("\n %c", ch);
    
    	printf("\n Type any character");
    	getchar(); // macro, will echo the char, requires enter key
    	//printf("\n %c", ch);
    
    	printf("\n Press Y or N");
    	fgetchar(); // function, will echo the char, requires enter key
    	//printf("\n %c", ch);
    
    	printf("\n I am here");
    
    	ch = 'A';
    	putch(ch);
    	putchar(ch);
    	fputchar(ch);
    	putch('z');
    	putchar('z');
    	fputchar('z');
    
    	return 0;
    }

  2. #2

  3. #3
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Yes the addition of these two lines just before fgetchar solves the problem

    [insert]
    Code:
    	puts("Flushing input");
    
    	while((ch = getchar()) != '\n' && ch != EOF);
    So the while loop makes sure that the input stream is cleared before i start taking any further input from the user. Is this necessary to do this everytime or only when i start facing such issues shoudl i have this mechanism for my program.

  4. #4
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    Is this necessary to do this everytime or only when i start facing such issues shoudl i have this mechanism for my program.
    Well, in theory you might predict them -- this is always a result of taking a single character from the input stream, so:

    scanf("%c",&byte);

    You type:

    x\n

    "\n" is left in the buffer. %d does not do this, for one reason or another.

    So another way to deal with this:

    scanf("%c%*c",&byte);

    %* discards the input, in this case the \n. You could also just use two fgetchar()s in a row, one unassigned. The while loop has some obvious advantages tho.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  5. #5
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Quote Originally Posted by MK27 View Post
    Well, in theory you might predict them -- this is always a result of taking a single character from the input stream, so:

    scanf("%c",&byte);

    You type:

    x\n

    "\n" is left in the buffer. %d does not do this, for one reason or another.

    So another way to deal with this:

    scanf("%c%*c",&byte);

    %* discards the input, in this case the \n. You could also just use two fgetchar()s in a row, one unassigned. The while loop has some obvious advantages tho.

    In %* c , is * the same meaning as thought it denotes a pointer ?

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by roaan View Post
    In %* c , is * the same meaning as thought it denotes a pointer ?
    Nope.

    Quote Originally Posted by GNU C Reference
    The conversion specifications in a scanf template string have the general form:

    % flags width type conversion

    In more detail, an input conversion specification consists of an initial `%' character followed in sequence by:

    - An optional flag character `*', which says to ignore the text read for this specification. When scanf finds a conversion specification that uses this flag, it reads input as directed by the rest of the conversion specification, but it discards this input, does not use a pointer argument, and does not increment the count of successful assignments.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Jun 2009
    Location
    US of A
    Posts
    305
    Thanks :-)

    That clears up a lot of things !!!!!

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by MK27 View Post
    Well, in theory you might predict them -- this is always a result of taking a single character from the input stream, so:

    scanf("%c",&byte);

    You type:

    x\n

    "\n" is left in the buffer. %d does not do this, for one reason or another.
    Of course %d does this too. The difference between %d and %c (which is even listed at that man page you posted) is that %d will skip any and all whitespace at the beginning of the input, so that the next time, when the input buffer now has \n175\n, the first \n will get discarded, 175 will get read in, and the \n is still there in the input buffer.

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, I'm wrong twice today
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM