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.
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.
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.
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.
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.
Anyway, as said, there are many ways to tackle a problem and sometimes we differ in our approaches. Thanks for the discussion Vectris.
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
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.
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.