C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-01-2009, 06:43 AM   #1
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 301
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;
}
roaan is offline   Reply With Quote
Old 08-01-2009, 07:03 AM   #2
Webhead
 
Spidey's Avatar
 
Join Date: Jul 2009
Posts: 278
Cprogramming.com FAQ > Flush the input buffer
__________________
Spidey out!
Spidey is offline   Reply With Quote
Old 08-01-2009, 07:29 AM   #3
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 301
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.
roaan is offline   Reply With Quote
Old 08-01-2009, 07:58 AM   #4
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 08-01-2009, 08:41 AM   #5
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 301
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 ?
roaan is offline   Reply With Quote
Old 08-01-2009, 08:51 AM   #6
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
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.
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Old 08-01-2009, 09:36 AM   #7
Registered User
 
Join Date: Jun 2009
Location: US of A
Posts: 301
Thanks :-)

That clears up a lot of things !!!!!
roaan is offline   Reply With Quote
Old 08-01-2009, 04:52 PM   #8
and the Hat of Guessing
 
tabstop's Avatar
 
Join Date: Nov 2007
Posts: 8,809
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.
tabstop is offline   Reply With Quote
Old 08-01-2009, 05:28 PM   #9
critical genius
 
MK27's Avatar
 
Join Date: Jul 2008
Location: SE Queens
Posts: 5,166
Okay, I'm wrong twice today
__________________

"A man can't just sit around." -- Larry Walters
MK27 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 02:12 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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