Thread: certain key to quit

  1. #1
    Registered User
    Join Date
    Dec 2011
    Posts
    18

    certain key to quit

    Hi..i have been trying to make my program to quit when specific key is pressed....how can i do that? at the moment when any key is pressed it will also quit.

    so far this is what ive done...

    Code:
    else if (ans == 'B')
                      {  
                    goto start;
                      }
                    
                    else if (ans == 'C')
                      {
                      quit;
                      }

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    you must have done more than that??

    a basic starting point is something of the form:
    Code:
    char ans;
    bool goAgain = true;
    
    while(goAgain)
    {
    	ans = GetAnswer();
    	if(ans == 'q')
    	{
    		goAgain = false;
    		ShowExitMessage();
    	}	
    }
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    Registered User
    Join Date
    Dec 2011
    Posts
    18
    what is the bool? where can i check about it more?

  4. #4
    Registered User
    Join Date
    Jan 2012
    Posts
    11
    Bool is a primitive data type as int,float.It only return two values:true or false.As in the:
    while(goAgain) equivalent to while(goAgain==1) with goAgain variable declare bool data type.
    Do I understand correctly your question?
    Sorry my english is very bad.

  5. #5
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    in case its useful the negative of the shorthand
    Code:
    while(goAgain)
    is simply to use the ' ! ' sign before your variable
    Code:
    while(!goAgain)
    while 'not' goAgain
    i.e while goAgain is not true.
    So you understand that in my first post i couldn't have used the ' not ' operator, as the loop would never have started, see if you can work out why.

    ps you can do the same sort of thing with ' if ' statements and plenty other stuff besides...
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Q to quit help
    By macman in forum C++ Programming
    Replies: 9
    Last Post: 04-12-2005, 03:30 PM
  2. I quit!
    By Luigi in forum C++ Programming
    Replies: 8
    Last Post: 12-03-2002, 09:30 AM
  3. Are Your Sure You Want To Quit? Help Please
    By paulroseby in forum C Programming
    Replies: 4
    Last Post: 10-23-2002, 04:34 PM
  4. Last try, then i quit...
    By Silentsharp in forum C++ Programming
    Replies: 15
    Last Post: 04-06-2002, 11:06 PM