Thread: Simple while loop - Windows 7

  1. #16
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by claudiu View Post
    Yes, this is aesthetically pleasing
    I know... sorry about that... but I don't have much tolerance for being blamed for stuff I didn't/wouldn't say or do.

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Ok, then... if you are finding fault with iMalc... don't quote me.
    I quoted you because you started arguing for changing the loop as being the fix. I typically quote who I'm replying to. I was replying to you, so I started quoting you.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I quoted you because you started arguing for changing the loop as being the fix. I typically quote who I'm replying to. I was replying to you, so I started quoting you.
    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?

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    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.

  5. #20
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    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"
    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?

  6. #21
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've explained about 10 times now what the problem was, and why I felt that way. I'm not going to do so again.
    Quote Originally Posted by CommonTater View Post
    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?
    Technically, you chose #3, because a do-while is not a while.


    Quzah.
    Last edited by quzah; 12-22-2010 at 09:49 PM.
    Hope is the first step on the road to disappointment.

  7. #22
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok you win.

  8. #23
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 02-09-2010, 06:08 PM
  2. Virtual keys
    By Arkanos in forum Windows Programming
    Replies: 4
    Last Post: 12-12-2005, 10:00 AM
  3. simple 2d "putpixel" api for windows?
    By spiky in forum C Programming
    Replies: 2
    Last Post: 10-27-2005, 02:44 PM
  4. Simple Windows Program - text?
    By Doagie in forum Windows Programming
    Replies: 1
    Last Post: 11-27-2004, 02:21 AM

Tags for this Thread