
"f flush" not working
#1
Posted 10 May 2010 - 09:11 PM
Register to Remove
#2
Posted 11 May 2010 - 11:44 AM
Edited by Vectris, 11 May 2010 - 11:47 AM.
"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield
#3
Posted 11 May 2010 - 11:50 AM
2.2 fflush() / fpurge() Issues
I'm bad. I should have never brought up the fflush() and fpurge() functions for clearing the text input stream. While they work, they're not proper programming and I'm advising against using them for now.
The real solution to immediate input is to use a library of functions that provide immediate input. For Unix there is the NCurses library, which is simple to learn and quite effective. In Windows, there are functions specific to the compiler as well as a battery of Windows API calls that provide immediate text input.
See section 2.2.2 for a good fflush() work-around.
http://www.c-for-dummies.com/faq/
Not sure how experienced you are with C but make sure your using it properly.
Edited by Vectris, 11 May 2010 - 11:50 AM.
"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield
#4
Posted 11 May 2010 - 12:53 PM
#include <stdio.h> int main() { char c,d; printf("Enter the character code for self-destruct?"); c=getchar(); fflush(stdin); printf("Input number code to confirm self-destruct?"); d=getchar(); fflush(stdin); if(c=='G' && d=='0') { printf("AUTO DESTRUCT ENABLED!\n"); printf("Bye!"); } else { printf("Okay. Whew!\n"); } return(0); }
What I'm trying to do is to get the appropriate messages shown by pressing "G" and "0" . The first printf function says to "Enter the character code to self destruct." I type in G and it sends me to the next message saying Input number code to self destruct. Instead of coming out by itself it comes stuck together with "okay whew" and doesn't give me a the option in which i press "0" and gives me the message "AUTO DESTRUCT ENABLED, bye!" My guess is that it reads the Enter key as the 2nd button. I've tried to put fflush under the 2nd printf function but alas to no avail.
Not very experienced at all. I'm actually starting to learn it USING C for dummies. I've already tried looking through their site, no help specifically to what i'm looking for.Not sure how experienced you are with C but make sure your using it properly.
#5
Posted 11 May 2010 - 01:36 PM
When you take out the first fflush the input that you gave to the first question is counted as the same input for the second question. However as long as you have fflush there it should be working, I have no idea why it the fflush wouldn't work for but work for me. Oh and the Enter doesn't matter, Enter is only counted as the confirm button for entering input so don't worry about that.
Judging from that website I linked in my last post, it may be one of the random errors that occurs when using fflush and that's why it's not recommended. I took a look at the work around and basically it's just a function to use in place of fflush, they call it jws_flush.
Try this:
#include <stdio.h> void jsw_flush ( void ) { int ch; /* getchar returns an int */ /* Read characters until there are none left */ do ch = getchar(); while ( ch != EOF && ch != '\n' ); clearerr ( stdin ); /* Clear EOF state */ } int main() { char c,d; printf("Enter the character code for self-destruct?"); c=getchar(); jsw_flush(); printf("Input number code to confirm self-destruct?"); d=getchar(); jsw_flush(); if(c=='G' && d=='0') { printf("AUTO DESTRUCT ENABLED!\n"); printf("Bye!"); } else { printf("Okay. Whew!\n"); } return(0); }
"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield
#6
Posted 11 May 2010 - 03:13 PM
#8
Posted 11 May 2010 - 11:44 PM
#10
Posted 14 May 2010 - 02:36 PM

Register to Remove
#11
Posted 14 May 2010 - 09:35 PM
Edited by Vectris, 19 May 2010 - 10:48 AM.
"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield
#12
Posted 18 May 2010 - 11:02 AM
#13
Posted 18 May 2010 - 07:16 PM
Don't have C here but maybe this will help.
Perhaps the char variables are expiring before they are being validated?
Try putting jsw_flush() after the condition has been validated and the result has been printed.
Proud graduate of WTT Classroom
The help we provide here is free, however, if you wish to donate, you can do so here: http://www.whatthetech.com/donate/
ASAP and UNITE member
________________________________________________
!
#14
Posted 19 May 2010 - 10:48 AM
I'm having a hard time following the whole 'end if' thing.
Ah I'm sorry, end if is a piece of BASIC coding that you must use to close an if statement. In C there is no end if. You can still use my psuedo code above though, just ignore the end ifs.
@inzanity
The char variables don't expire, they hold their values during runtime. It's just that she doesn't test the variables to see if she should display the second prompt. jsw_flush() is simply a command to clear the input cache (probably not a proper term but it fits for what I'm trying to explain). Any time the program gets input, you need to use jsw_flush() right afterward to clear the input or else the next time you try to get input, it will be the same thing regardless of what the user actually inputted. So putting jsw_flush() after the condition has been printed would mess up the second condition. In order to get a second condition you have to clear the input using jsw_flush() first.
Edited by Vectris, 19 May 2010 - 10:56 AM.
"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield
#15
Posted 19 May 2010 - 06:58 PM

Proud graduate of WTT Classroom
The help we provide here is free, however, if you wish to donate, you can do so here: http://www.whatthetech.com/donate/
ASAP and UNITE member
________________________________________________
!
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users