Thread: Simple while loop - Windows 7

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    9

    Red face Simple while loop - Windows 7

    I am learning C using C for dummies 2nd Edition. I'm using codeblocks 10.05 with mingw (GCC) on Windows 7 Ultimate (64bit).

    When I compile and run the code below on Windows XP everything works as expected and I can input from the keyboard fine and the loop does not exist until I press ~ followed by Return.

    Unfortunately in Windows 7 running the code as a normal user or as administrator it, appears to, just totally ignore the while loop, prints the other messages to screen and exits the program.

    Is there a problem with this code on Windows 7?

    Code:
    #include <stdio.h>
    
    int main()
    {
       char ch;
    
       puts("Start typing");
       puts("Press ~ then Enter to stop");
    
       while(ch!='~')
       {
          ch=getchar();
       }
    
       printf("Thanks!\n");
       return(0);
    
    }
    Thanks in advance.

  2. #2
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    You are never initializing ch, so it could be ~.
    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.

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    9
    I found the solution as a FAQ linked below. I should have declared ch as an int not a char.

    Question 12.1

    Thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I doubt that was the reason your loop "didn't work". A tilde is a valid 'char' type value.


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

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Try it like below.... The problem is that you were testing ch before it had any legitmate value assigned...


    Code:
    #include <stdio.h>
    
    int main()
    {
       char ch;
    
       puts("Start typing");
       puts("Press ~ then Enter to stop");
    
       do
       {
          ch=getchar();
       }
       while(ch!='~');
    
       printf("Thanks!\n");
       return(0);
    
    }

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Quote Originally Posted by philuk2000 View Post
    I found the solution as a FAQ linked below. I should have declared ch as an int not a char.

    Question 12.1

    Thanks
    NO, that is not the cause of the problem here. 12.1 is not relevant to the code you have because you are not comparing against EOF. Changing it to int only makes it a bit less likely that it will happen to already equal the value it's looking for. The bug can still happen though.

    The real fix is given in CommonTater's post. You were using the wrong kind of loop.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by iMalc View Post
    The real fix is given in CommonTater's post. You were using the wrong kind of loop.
    The loop wasn't the problem. Using the variable before you initialize it was. You could also compile with warnings higher, and the compiler would likely tell you as much.


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

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The loop wasn't the problem. Using the variable before you initialize it was. You could also compile with warnings higher, and the compiler would likely tell you as much.
    Yep. By changing the loop so ch is assigned before it's tested we eliminated the problem.

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by CommonTater View Post
    Yep. By changing the loop so ch is assigned before it's tested we eliminated the problem.
    I know how you fixed the problem. But the loop wasn't the problem. He could have just as easily done:
    Code:
    for( c = getchar(); c != '~'; c = getchar() );
    Or...
    Code:
    while( (c = getchar()) != '~' );
    Or one of half a dozen other loop variations. The loop wasn't the reason it didn't work.

    You are both saying "The reason it worked is because I pushed the car backwards to the gas station!" No, it doesn't matter how you get to the gas station. The fact is, you had no gas. It doesn't matter how you get there, or if even you just bring a gas can to the station, then bring the can of gas home and fill up your car there. The problem was you were out of gas. It had nothing to do with how the car got to the gas.

    If you still don't understand, consider this: All I have to do to fix the first program is to add 2 characters. You don't have to rewrite the loop.


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

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The loop wasn't the reason it didn't work.
    That's right.

    It didn't work because the first time he tested ch it was not intialized with any valid value.

    Changing the loop was how I chose to fix the problem.

    And yes he could have also done char ch = 0; adding two characters as you suggested.
    I chose a somewhat different approach... why is that a problem?

  11. #11
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem with what you are saying, is what you are saying the actual problem was. The problem he had wasn't the loop. The problem was checking the variable before you assign it a value. You should be teaching people to initialize their variables before they use them. The loop has nothing to do with the actual problem. There could have been no loop involved, and the cause of problem would have still been the same.
    Code:
    char c;
    if( c != '~' )
        printf( "You still have the same problem.\n" );

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

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    The problem with what you are saying, is what you are saying the actual problem was. The problem he had wasn't the loop.
    Ok... one more time for clarity...

    1) I did not, would not and will not say the loop was the problem.
    2) No the loop was not the problem.
    3) The problem he had was not the loop.
    4) The loop didn't cause the problem he was having.

    OK?

    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.

    OK?

    I chose to suggest he fix the problem in a certain way.
    Why exactly do you have a problem with that?

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I didn't say you did it wrong. I said this is wrong:
    Quote Originally Posted by iMalc View Post
    The real fix is given in CommonTater's post. You were using the wrong kind of loop.
    He wasn't "using the wrong kind of loop". He was using his variable before he initialized it. I apologize for making it seem like you didn't understand the difference. I got my posters confused. The issue wasn't the loop; rather it was as you stated here:
    Quote Originally Posted by CommonTater View Post
    Try it like below.... The problem is that you were testing ch before it had any legitmate value assigned...

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

  14. #14
    Registered User claudiu's Avatar
    Join Date
    Feb 2010
    Location
    London, United Kingdom
    Posts
    2,094
    Quote Originally Posted by CommonTater View Post
    Ok... one more time for clarity...

    1) I did not, would not and will not say the loop was the problem.
    2) No the loop was not the problem.
    3) The problem he had was not the loop.
    4) The loop didn't cause the problem he was having.

    OK?

    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.
    The problem was that he was using the variable before it was initialized with a valid value.

    OK?

    I chose to suggest he fix the problem in a certain way.
    Why exactly do you have a problem with that?
    Yes, this is aesthetically pleasing
    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.

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by quzah View Post
    I didn't say you did it wrong. I said this is wrong:
    He wasn't "using the wrong kind of loop". He was using his variable before he initialized it.

    Quzah.
    Ok, then... if you are finding fault with iMalc... don't quote me.

    GEES

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