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 91867 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

#16 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 19 May 2010 - 09:53 PM

I don't know much about C but if getchar() returned an integer then I don't know how her condition would work.

if(c=='G' && d=='0')

Looking only at c==G, if the input stored in 'c' was an integer, then how could this condition ever evaluate to true? Yet it does during runtime if the user enters G. I realize the website I got the jsw_flush code from also says that getchar() returns an int so maybe I'm missing something. But the way I see it, you can't compare an int value to a string value in a condition like in the code.
The internet's most useful tool, Google.

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

    Advertisements

Register to Remove


#17 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 19 May 2010 - 10:25 PM

Yes it also says here. Am also not that familiar with C but maybe it is doing some kind of typecasting on the condition.

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

________________________________________________


!


#18 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 19 May 2010 - 10:29 PM

Ah, according to wiki, "characters can be used as integers".

This may be the reason why the condition is not working as intended.

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

________________________________________________


!


#19 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 20 May 2010 - 11:27 AM

Ah, according to wiki, "characters can be used as integers".

This may be the reason why the condition is not working as intended.


Nice find, but don't you mean the reason why it is working? Actually, I assumed maybe we were getting confused about the discussion and which problem we were attributing it to. Let me clear up what I was talking about. Two things:

1) The OPs current problem. When she inputs something other than 'G' for the first prompt, she doesn't want the second prompt to display. This isn't really an error of any kind, her code is running fine. She just needs to add a condition to the second prompt to check for what input was given in the first prompt.

2) The fact that even though getchar() is an integer type, it still accepts character values. And when you use a condition which compares the integer value stored inside a char variable to an actual char value, it still works.

I believe you found the solution to 2) though. Characters can be passed as a type of int and thus the conditions using them work fine. I'm very certain that #2 has nothing to do #1 though, so declaring the variables c and d as characters instead of integers wouldn't effect the OPs current problem. It would however be good coding practice to declare them as int to keep a consistent variable type.

Edited by Vectris, 20 May 2010 - 11:30 AM.

The internet's most useful tool, Google.

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

#20 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 20 May 2010 - 07:38 PM

I thought I was missing something as I was confused :lol:

Just reread the thread and installed C. :blush:

Yes, the suggestion on post #15 is working.

@Kariya

Here is the somewhat reworded pseudo code presented by vectris:

ask for the characted code;

if the character code = 'g' then 
   ask for the number code;

   if number code = '0' then
	  display("AUTO DESTRUCT ENABLED!");
	  display("Bye!");
   else display ("Okay. Whew!");		<- displays if the number code is wrong.
else display ("Okay. Whew!");		   <- displays if the character code is wrong, it won't ask for the number code.

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

________________________________________________


!


#21 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 21 May 2010 - 09:35 AM

There's no problem with that code but it's a better coding practice to form "if" statements so that you do not have to have two separate pieces of code that do the same thing simply because there are 2 ways to get to it. In this instance the "Phew" message was in the code twice because it will display if the user enters the wrong code either time. Instead, test for both values and you can simplify the code to one if else statement.

ask for the characted code;

if the character code = 'g' then
   ask for the number code;

   if character code = 'g' and number code = '0' then
	  display("AUTO DESTRUCT ENABLED!");
	  display("Bye!");
   else display ("Okay. Whew!");		<- displays if either the character code or number code is wrong.

The internet's most useful tool, Google.

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

#22 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 21 May 2010 - 09:03 PM

Yes, there are many ways to do a program. In my example above, there is no need to test for the first condition twice as it was already tested in the first 'if' statement, the second 'if' statement only test if the number code is correct as the statement is inside the first 'if' statement. So, if the first 'if' statement is wrong, it will display 'whew' and will exit the program as what I understand Kariya wants. If it's correct, then it will validate the second condition (if number code = '0'), then display the appropriate message depending on the input. Hope am making sense here. :lol: Anyway, as said, there are many ways to tackle a problem and sometimes we differ in our approaches. Thanks for the discussion Vectris. :thumbup:

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

________________________________________________


!


#23 jpshortstuff

jpshortstuff

    Teacher Emeritus

  • Authentic Member
  • PipPipPipPipPipPip
  • 5,710 posts

Posted 22 May 2010 - 01:16 AM

I think you are both onto good points here. As you have stated, neither of your methods are 'wrong', and there are many 'right' ways to do the program.

You could combine your two methods by using an additional boolean. This would allow you to only evaluate the character code once (as in inzanity's method), and also only have your 'destruct' code in one place (as in Vectris' method):
boolean destruct = false

ask for the character code
if character code = 'g' then {
	ask for the number code;
	if number code = '0' then
		destruct = true
}

if destruct
	display "AUTO DESTRUCT ENABLED!"
	display "Bye!"
else
	display "Okay. Whew!"

exit

This makes it very easy to change the logic if you need to add new conditions to your destruct system, without having to move code around.

Anyway, nobody is wrong, but this just goes to show how many ways there are to hammer a nail in :)

-jp

Proud Graduate of the TC/WTT Classroom

At weekends (GMT) I may not be able to reply promptly due to various commitments. Please be patient and I will respond as soon as I can.

My help is free, however, if you wish to make a small donation to show appreciation and to help me continue the fight against Malware, then click here Posted Image

Need help remembering those important computer maintenance tasks? Let SCars do it for you.

Posted Image

#24 inzanity

inzanity

    ♠♠lost♠♠

  • Malware Team
  • 2,340 posts

Posted 22 May 2010 - 02:33 AM

:thumbup:

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

________________________________________________


!


#25 DanielD

DanielD

    Silver Member

  • Visiting Tech
  • PipPipPip
  • 342 posts

Posted 22 May 2010 - 01:23 PM

Ah I didn't realize I was evaluating twice. When it comes down to it I think the boolean method would be the best, and it would be much easier to add onto later. Nice job jpshortstuff.
The internet's most useful tool, Google.

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

    Advertisements

Register to Remove

Related Topics



0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users