![]() |
| | #1 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 301
| fgetchar() doesnt work [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 | |
| | #2 |
| Webhead Join Date: Jul 2009
Posts: 278
|
__________________ Spidey out! |
| Spidey is offline | |
| | #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);
|
| roaan is offline | |
| | #4 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Quote:
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. | |
| MK27 is offline | |
| | #5 | |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 301
| Quote:
In %* c , is * the same meaning as thought it denotes a pointer ? | |
| roaan is offline | |
| | #6 | |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Nope. Quote:
| |
| MK27 is offline | |
| | #7 |
| Registered User Join Date: Jun 2009 Location: US of A
Posts: 301
| Thanks :-) That clears up a lot of things !!!!! |
| roaan is offline | |
| | #8 |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,809
| 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 | |
| | #9 |
| critical genius Join Date: Jul 2008 Location: SE Queens
Posts: 5,166
| Okay, I'm wrong twice today |
| MK27 is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
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 |