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

This is a discussion on while (inp != 'q') q key doesnt brake loop within the C++ Programming forums, part of the General Programming Boards category; Code: char input; while (input != 113 || input != 27){ //Ask for user input cin >> input; switch (input){ ...

  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
    32,822
    > 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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 04:37 AM.

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,761
    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, 08:07 AM
  2. why this while condition doesnt stop the loop??
    By transgalactic2 in forum C Programming
    Replies: 40
    Last Post: 04-07-2009, 09:38 AM
  3. best way to brake a while(TRUE) loop?
    By g1i7ch in forum C Programming
    Replies: 6
    Last Post: 07-10-2006, 03:58 AM
  4. My loop doesnt work
    By Evandb in forum C Programming
    Replies: 3
    Last Post: 05-18-2004, 12:04 AM
  5. doesnt work while ..loop
    By condorx in forum C Programming
    Replies: 3
    Last Post: 02-24-2002, 12:53 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21