That is ONE way to fix the problem. There was no intervening code and nothing else that would be affected by the solution I offered so I see no harm in it.
I STILL don't understand why you think this is so evil....
Or did you, perhaps, miss the part in message #8 where I agreed with you?
The problem is with the way the "fix" was described to someone new to C. When you're teaching someone C, you should be clear what the exact issue is. If that was a pointer, he would likely be crashing his program, instead of just getting weird loop behavior. The real answer to his problem is: "initialize your variable before you use it", not "rewrite your loop so it checks later". That's the problem I have with the provided solutions, and iMalc's post.
Don't tell them to rewrite their loop, just tell them to initialize their variables before they use them. He's not going to understand why he's rewriting his loops all the time, if he doesn't understand that he simply could have avoided the whole thing, if he would have simply initialized the variable to some value before he started fiddling with it. (I know that, and you know that, and I know that you know, and you know that I know. But HE doesn't know, and he was told "the fix is to rewrite your loop". It's not. It's to initialize his variables before he checks their value. Rewriting the loop has a side effect of fixing the issue - the actual fix is assigning a value to the variable before you start checking the variable for values.)
He needs to get the point that he should be initializing it, not that he should be rewriting his loops. The loop is irrelevant. He encountered the problem because he tried to see what was in the variable, before he gave the variable a value. Teach good fundamentals. Assign a variable a value before you check to see what value a variable has.
I am pedantic by nature when it comes to explaining or having things explained. You should know why something happens, not just how to avoid it.
Quzah.
Last edited by quzah; 12-22-2010 at 08:58 PM. Reason: tl;dr - don't argue with a pedant.
Hope is the first step on the road to disappointment.
From Post #5.... Top line...
"Try it like below.... The problem is that you were testing ch before it had any legitmate value assigned..."
Now as I see it he has 3 choices here...
1) Initialize the variable explicitly... char ch = 0;
2) Make a quick change in his loop so he's not testing an uninitialized variable.
3) Write an entirely different loop.
I chose #2 and explained why I chose #2... Why is this a problem?
Last edited by quzah; 12-22-2010 at 09:49 PM.
Hope is the first step on the road to disappointment.
Ok you win.
Guys, relax !
The OP hasn't even been around to check this interesting debate.
1. Get rid of gets(). Never ever ever use it again. Replace it with fgets() and use that instead.
2. Get rid of void main and replace it with int main(void) and return 0 at the end of the function.
3. Get rid of conio.h and other antiquated DOS crap headers.
4. Don't cast the return value of malloc, even if you always always always make sure that stdlib.h is included.