Thread: while (inp != 'q') q key doesnt brake loop

  1. #1
    Registered User
    Join Date
    May 2012
    Posts
    71

    while (inp != 'q') q key doesnt brake loop

    Code:
    char input;
    
    while (input != 113 || input != 27){
    
            //Ask for user input
            cin >> input;
            switch (input){
    
    
                case 113: //(Q key)
                    cout << "startGame ";
                    break;
    
    
                case 27: //(ESC key)
                    cout << "exitGame ";
                    break;
    
    
                default:
                    cout << "default ";
                    break;
    
    
            } //switch (input) ends
    
    
        } //while (input != Q || input != ESC) ends
        
        cout << "out" << endl;
    I press Q key or ESC but the while loop keeps repeating the switch
    I really have no idea whats going on

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > while (input != 113 || input != 27)
    You need another condition - it can't be NOT equal to both values at the same time.
    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.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    71
    But i say || o_o
    I added this
    Code:
    bool menu;
    
    SetMenu (true);
        while (GetMenu()){...}
    and in the case's i add
    Code:
    SetMenu(false);
    a simple bool

    Tnx Salem
    Last edited by CoffeCat; 05-07-2012 at 03:37 AM.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Well if you fixed it then I guess there is no point... anyway the OR was exactly the problem. Your understanding of OR and AND are backwards. when you want both operand to be true you're supposed to use AND as in while (key != 'Q' && key != ESC) ...

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    71
    Yes they are confusing me lol, though they sound extremly easly...

    Thanks alot

    PS; badass song ^_^

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. why the first loop doesnt stop..
    By transgalactic2 in forum C Programming
    Replies: 1
    Last Post: 06-20-2009, 07:07 AM
  2. why this while condition doesnt stop the loop??
    By transgalactic2 in forum C Programming
    Replies: 40
    Last Post: 04-07-2009, 08:38 AM
  3. best way to brake a while(TRUE) loop?
    By g1i7ch in forum C Programming
    Replies: 6
    Last Post: 07-10-2006, 02:58 AM
  4. My loop doesnt work
    By Evandb in forum C Programming
    Replies: 3
    Last Post: 05-17-2004, 11:04 PM
  5. doesnt work while ..loop
    By condorx in forum C Programming
    Replies: 3
    Last Post: 02-24-2002, 12:53 PM