How to Create a 20 Questions Game in C++

This tutorial will walk you through creating 20 Questions in C++ with numbers using Visual Studio. This tutorial is very 'bare bones' and only uses the basics of C++ programming. Obtain a copy of Visual Studio and open it.

Part 1 of 3:

Creating a Project

  1. Picture 1 of How to Create a 20 Questions Game in C++
    Obtain a copy of Visual Studio and open it.
    1. You can find a detailed guide to setup at How to Install and Setup Visual Studio Express 2013.
  2. Picture 2 of How to Create a 20 Questions Game in C++
    Create a project by clicking the file tab on the top left and clicking New Project.
  3. Picture 3 of How to Create a 20 Questions Game in C++
    Click the Templates tab on the left.
  4. Picture 4 of How to Create a 20 Questions Game in C++
    Click Visual C++ under the templates tab.
  5. Picture 5 of How to Create a 20 Questions Game in C++
    Click Empty Project in the middle.
  6. Picture 6 of How to Create a 20 Questions Game in C++
    Name your project something relevant.
    1. For example: 20 Questions Game.
  7. Picture 7 of How to Create a 20 Questions Game in C++
    Add a source file. Right-click Source Files on the right side of the screen under the solution explorer box. Then hover add and click New File.
    1. Source Files -> Add -> New File
  8. Picture 8 of How to Create a 20 Questions Game in C++
    Select C++ file(.cpp), name it something relevant and click Add.
    1. For example: "Main.cpp" because this will be our main source file. This naming scheme is especially important in larger scale programs that require more than one file.
Part 2 of 3:

Setting Up the Program

  1. Picture 9 of How to Create a 20 Questions Game in C++
    Begin the programming setup.
    1. Type: #include
      1. This includes a file in the C++ library that allows console manipulation.
    2. Type: using namespace std;
      1. This means you're using a standard (std) namespace.
    3. Type: int main(){ }
      1. This is the main function that the program will run. Everything runs through this.
      2. In between the curly braces { }, press enter a few times. Everything goes in between the curly braces. Note: The green colored words are comments. These are for you (the interpreter) to make better sense of the code.
  2. Picture 10 of How to Create a 20 Questions Game in C++
    Declare the variables.
    1. Within the main function brackets,(int main()), create the following variables:
      1. int max = 100;
      2. int min = 0;
      3. char ans;
      4. int num = 0;
      5. int guess;
      6. int numGuess = 0;
        1. Note that some variables are declared with values, while others are not. This is because those variables are required by the program to be predefined. This is determined by how they're used.
  3. Picture 11 of How to Create a 20 Questions Game in C++
    Create the message to the user.
    1. Type: cout << 'Think of a number between 1 and 100.' << endl;
      1. This prompts the user for their number, giving them an idea of what is required of them.
Part 3 of 3:

Programming the Logic

  1. Picture 12 of How to Create a 20 Questions Game in C++
    Create the do-while loop. This will control all of the game logic.
    1. Type: do{ }while(num == 0 && numGuess < 20);
    2. Press Enter a few times between the braces. Note: num == 0 && numGuess < 20 basically means the loop will continue until num equals 0 AND numGuess is less than 20.
  2. Picture 13 of How to Create a 20 Questions Game in C++
    Type everything in the picture within the do-while brackets.
    1. Understand the logic of the loop:
      1. The user will enter Y or N, based on their number.
      2. If their number is greater than or equal to guess, numGuess increments by 1 and the program makes a guess.
        1. If the guess is correct, the program breaks out of the loop and the computer wins.
        2. If the guess is incorrect, min = guess; effectively cutting the range of values in half and starting the loop over again.
      3. If their number is not greater than or equal to guess, numGuess increments by 1 and max = guess; cutting the range of values in half and starting the loop over again.
      4. The program will go through these guesses and checks until it narrows the users number down to a single number or it reaches its 20 question limit.
    2. Save and run the program. At this point, it should do everything it's intended to do, excluding the concluding message. If the program suddenly closes when it guesses your number or when it fails to guess your number, that's normal. We will fix this in the next few steps.
  3. Picture 14 of How to Create a 20 Questions Game in C++
    Type everything in the picture after the do-while loop. This will be the concluding lines of code that handle whether or not the player wins.
    1. Understand the logic of the concluding message:
      1. If the numGuess equals 20 and num equals 0, the computer could not guess you number.
        1. Note that the value of num will never change if the users number is never guessed.
      2. If the users number is guessed correctly, the computer will output your number and a little victory smiley face.
      3. Note the lines system("pause"); and return 0;
        1. system("pause") simply pauses the program, allowing the user to read the message.
        2. return 0; exists because it is good practice to return a value in the main function; even if the value is irrelevant.
  4. Picture 15 of How to Create a 20 Questions Game in C++
    Review your completed program.
Update 05 March 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile