Thread: Guessing game: how to quit the game?

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    2

    Guessing game: how to quit the game?

    Hello Everyone,
    I am writing a guessing game using for loop but did not know how to quite the game at any stage that I want. This is an example of my for loop:

    ...............................................
    case 1:
    askquestion_1( guesses, "\nAttempt %d: Is your animal a dog? ", dog );
    break;
    case 2:
    askquestion_2( guesses, "\nAttempt %d: Is your animal a mouse? ", mouse );
    break;
    .........................................

    How can i quiet after the first break. Can anyone help?


    Thanks.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    use a flag and check it in the loop condition
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    I suppose questions are used in a loop. So provide a means to exit that loop at any iteration.

    From the looks of the snippet it also seems that you are hard-coding things that should be handled by loops and arrays.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Dec 2008
    Posts
    2
    Could you please give me an example.

    what do you mean by: use a flag and check it in the loop condition


    Thank you.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int bFlag = 1;
    int index;
    for(index = 0; bFlag && index <10 ; index++)
    {
       if(index == 5)
          bFlag = 0;
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  6. #6
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Code:
    int user_wants_to_continue = 1;
    do
    {
        guessed = ask_question();
        if (user_wants_to_abort) user_wants_to_continue = 0;
    } while (!guessed && user_wants_to_continue);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 20q game problems
    By Nexus-ZERO in forum C Programming
    Replies: 24
    Last Post: 12-17-2008, 05:48 PM
  2. Need book to program game into multiplayer...
    By edomingox in forum Game Programming
    Replies: 3
    Last Post: 10-02-2008, 09:26 AM
  3. Game Designer vs Game Programmer
    By the dead tree in forum Game Programming
    Replies: 8
    Last Post: 04-28-2005, 09:17 PM
  4. number guessing game, no idea where to start
    By karin in forum C Programming
    Replies: 9
    Last Post: 03-18-2004, 09:49 PM
  5. Number guessing game
    By rmathus in forum C++ Programming
    Replies: 2
    Last Post: 03-02-2004, 02:11 PM