This is a read-only archive. No new posts or registrations. Privacy Page Still need help? Ask the volunteers at Geeks to Go, free.
Software

"f flush" not working

11 min read

This thread's last reply is from . Advice, software, and links below may be out of date — treat specific steps and download links with caution.

I"m trying out a program that shows how to use "IF" and having a difficult time making the program do what I want.: printf("Enter the character code for self-destruct?"); c=getchar(); fflush(stdin); printf("Input number code to confirm self-destruct?"); d=getchar(); if(c=='G' && d=='0') { printf("AUTO DESTRUCT ENABLED!\n"); printf("Bye!"); } else { printf("Okay. Whew!\n"); } I can't get "fflush" to work, so when I execute the program, the 2nd and 3rd printf string come stuck together. I've tried replacing fflush with "fpurge" but the compiler doesn't understand it. Does anyone know if i'm missing something?
I have no experience with C so forgive me if I'm stating something obviously wrong. You say the problem is that the 2nd and 3rd printf come stuck together, I'm guessing printf produces a form of output with your string. I'm also assuming that fflush is a way to erase that output and that's why you expect the 3rd printf to appear alone because the 2nd one should be cleared. If so, then I think you forget to add another fflush() after the 2nd printf. You have "fflush" after the first printf but it doesn't appear after the 2nd printf so it looks to me like you just need to use the command again. Also, if I'm wrong, perhaps giving a little more info on what "fflush" and "printf" are supposed to do. Including what you mean by "2 and 3" being "stuck together" would help. I don't believe there are as many programmers as general computer techs out there and even fewer in this specific language so giving as much info as possible could help people like me who program, but not in the same language, lend a hand.
Also just something I found about using the fflush and fpurge commands.

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.
Sorry for the sloppy detail. Let me try and explain it better. This is the whole program:

#include 

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 sure how experienced you are with C but make sure your using it properly.

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.
I have a C++ studio and tested out the code and it works fine. I was able to reproduce the exact error you said was happening though by removing the very first fflush.

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 

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);
}
Hm. I saved the new program and ran it. Although it didn't run as i assumed it woud, it still ran better than the one I wrote. Now i need to look at the other functions so that i can understand it better. Thank you for your help. Oh one other thing. I noticed that whenever you type in anything else that isn't a 'G', it still asks for 0(zero). Is there anything i can put so that it skipps the printf statement that asks for the "number code" and go straight to the "okay whew" printf statement?
Okay, I got it now. It works better now, but just needs ONE more tweak. I added the IF to the 1st printf statement so I got the two statements to give me "okay whew" when the wrong button was pressed. The only thing screwy is, whenever you press the wrong button on 1st printf, you still get the second "okay whew" from the 2nd printf. Is there anyway i can get it ignored when the wrong button is pressed the first time?
I'm sorry. I re-read my earlier post and realized I worded my problem wrong. :smack: What I meant to say was that whenever I type in the wrong letter for the 1st printf, I still get prompted to type in the number (from the 2nd printf). I want the program to go straight to "okay whew" when the wrong button is pressed instead of having to wait for then number(hope that makes more sense.) Here is my code: #include 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); }
I'll let you write the code but here's the structure Printf 1 If character is correct then Printf 2 If printf 1 is correct and printf 2 is correct then display auto destruct else display ok whew Just organize your if statements like that and it should work fine.
Hi,

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.

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.
@vectris Thanks for clearing that tup. :thumbup: Doesn't getchar() return an int? If so, then shouldn't the variable c and d be also declared as int instead of char?

Still having this problem?

This topic is from the What the Tech archive and can't take new replies. Geeks to Go is an active, free help community run by volunteers. Post your question there and a person, not a bot, will work through it with you.

Ask on Geeks to Go →