Jump to content

Build Theme!
  •  
  • Infected?

WE'RE SURE THAT YOU'LL LOVE US!

Hey there! :wub: Looks like you're enjoying the discussion, but you're not signed up for an account. When you create an account, we remember exactly what you've read, so you always come right back where you left off. You also get notifications, here and via email, whenever new posts are made. You can like posts to share the love. :D Join 93116 other members! Anybody can ask, anybody can answer. Consistently helpful members may be invited to become staff. Here's how it works. Virus cleanup? Start here -> Malware Removal Forum.

Try What the Tech -- It's free!


Photo

"f flush" not working


  • Please log in to reply
24 replies to this topic

#1 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 10 May 2010 - 09:11 PM

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?

    Advertisements

Register to Remove


#2 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 11 May 2010 - 11:44 AM

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.

Edited by Vectris, 11 May 2010 - 11:47 AM.

The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#3 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 11 May 2010 - 11:50 AM

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.

Edited by Vectris, 11 May 2010 - 11:50 AM.

The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#4 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 11 May 2010 - 12:53 PM

Sorry for the sloppy detail. Let me try and explain it better. This is the whole program:

#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 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.

#5 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 11 May 2010 - 01:36 PM

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 <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);
}

The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#6 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 11 May 2010 - 03:13 PM

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?

#7 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 11 May 2010 - 03:31 PM

Sure, just include another if statement before your printf #2. If c = G then ask second question.
The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#8 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 11 May 2010 - 11:44 PM

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?

#9 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 12 May 2010 - 11:10 AM

Based on the changes you made, that shouldn't be happening. Post your code and I'll take a look at it.
The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#10 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 14 May 2010 - 02:36 PM

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: [codebox]#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); }[/codebox]

    Advertisements

Register to Remove


#11 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 14 May 2010 - 09:35 PM

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.

Edited by Vectris, 19 May 2010 - 10:48 AM.

The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#12 Kariya

Kariya

    New Member

  • Authentic Member
  • Pip
  • 15 posts
  • Interests:Music, video games, computers, anime

Posted 18 May 2010 - 11:02 AM

I'm having a hard time following the whole 'end if' thing.

#13 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 18 May 2010 - 07:16 PM

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.

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 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

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.

The internet's most useful tool, Google.

"Life's like a game of Poker, sometimes you win, sometimes you lose" - Garfield

#15 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 19 May 2010 - 06:58 PM

@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?

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

________________________________________________


!

Related Topics



1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users