Thread: Can't figure out this doesn't work: While e or E is not pressed

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    13

    Can't figure out this doesn't work: While e or E is not pressed

    I have a menu displayed, if the user presses H or h, it does something.

    If the user presses S or s, it does something else.

    If the user presses E or e, the program exits.

    This works: while ((ch = getch()) != 'e')

    but not the following:

    Code:
        while ((ch = getch()) != 'e' || (ch = getch()) != 'E' )
        {
            if (ch == 'H' || ch == 'h' ) {
                printf("asdf\n");
            }
            else if (ch == 'S' || ch == 's' ) {
                    printf("erwer\n");
            }
            else {    
                printf("Will display status by default\n");
            }
    
        }

    The terminal just seems to lock up.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    while ((ch = getch()) != 'e' || (ch = getch()) != 'E' )
    Each one of those reads a different key. Which actually fixes one problem you would be having, but ruins all the rest of it.

    When you type E to exit, you also hit enter. First, ch gets assigned E, then it gets assigned ENTER with the second call to getch. You should be doing something like:
    Code:
    while( (c = getch()) != 'e' && c != 'E' )
    {
        ...
    }
    Now you could just use the toupper or tolower functions as well, and just check once, but it all works out the same in the end. However, after you do any of what I've described, you still need to take care of that ENTER key you've just typed.


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

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    You call getch() twice. The first time it grabs what you enter, the second time it's probably waiting for another key press (note, getch is non-standard, so I don't really know what it does). Also, you probably want an && in your while loopTry:
    Code:
    while ((ch = getch()) != 'e' && ch != 'E') {
        ...
    }
    Last edited by anduril462; 10-13-2011 at 05:35 PM. Reason: Bug fix, noticed when I saw Quzah's reply

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Or even
    ch = tolower(getch())
    if you don't care about case at all later on, and it would simplify all the later conditions.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-07-2010, 06:53 AM
  2. Can't figure out why this program won't work
    By Northstar in forum C Programming
    Replies: 6
    Last Post: 11-26-2007, 11:31 AM
  3. Can't figure out why it won't work
    By array in forum C++ Programming
    Replies: 2
    Last Post: 04-14-2004, 12:29 PM
  4. I can't figure out why this doesn't exit the loop, MAN!
    By Shadow12345 in forum C++ Programming
    Replies: 2
    Last Post: 10-02-2002, 08:47 AM
  5. my function doesn't work! it should work
    By Unregistered in forum C Programming
    Replies: 13
    Last Post: 05-02-2002, 02:53 PM