= Guessing game in C++ Learning a programming language often ends up being an exercise either learning some basic concepts of computer code, or, if you already know how to code in some other language, re-learning how these concepts are expressed in a new language. A convenient way to learn these principles is to create a simple guessing game. This forces you to learn the way a language receives input and sends output, how it compares data, how to control the flow of a program, and how to leverage conditionals to affect outcome. And of course, it also ensures that you know how a language structures its code (for instance, Lua or Bash can easily run as a script, while LINK-TO-JAVA-GUESS[Java] requires the creation of a class), In this article, I demonstrate first how to implement a guessing game for the terminal in C++. == Installing dependencies To follow along with this article, you need C++ and a compiler. On Linux, you can get everything you need by installing the *Qt Creator* IDE from your distribution's software repository: For instance, on Fedora, CentOS, or RHEL: ----[source,bash] $ sudo dnf install qt-creator ---- On Debian, Ubuntu, Chromebook, or similar: ----[source,bash] $ sudo apt install qtcreator ---- This article doesn't utilize the *Qt Creator* IDE, but it's an easy way to get everything you need installed, and for complex C++ projects (including those with a GUI), it's an essential tool to have. On MacOS or Windows, follow installation instructions on http://qt.io[qt.io]. == Includes and namespace The core language of C++ is minimal. Even a simple application requires the use of additional libraries. For this application, I use *streamio* to gain access to the *cout* and *cin* keywords. I also establish that the program uses the *std* namespace. This isn't strictly necessary, but without setting the namespace to *std*, all keywords from the *iostream* library required a namespace prefix. For instance, instead of writing *cout*, I would have to write *std::cout*. ----[source,c++] #include <iostream> using namespace std; ---- Statements in C++ are terminated witha semi-colon. == Class Ever C++ application requires at least one function. The primary function of a C++ application must be called *main*, and it must return an integer (*int*), which corresponds to the LINK-TO-WHAT-IS-POSIX-ARTICLE[POSIX] expectation that a process returns 0 upon success and something else upon failure. You create a new function by providing its return type, and then its name: ----[source,c++] int main() { // code goes here } ---- == Program logic For the game code itself, you must first establish a random number to be the target of the player's guesses. You do this in C++ by establishing a _seed_ for pseudo-random number generation. A simple seed is the current time. Once the seed has been started, you retrieve a number between 1 and 100 by calling the *rand* function with an upper contraint of 100. This actually generates a random number from 0 to 99, so you add 1 to whatever number has been chosen, and assign the result to a variable called `number`. You must also declare a variable to hold the player's guess. For clarity, I call this variable `guess`. In this sample code, I also include a debug statement that tells you exactly what the random number is. This isn't particularly good for a guessing game, but it makes testing a lot faster. You can remove the line, or just comment it out by prefacing it with `//`, later. Statements in C++ are terminated witha semi-colon. ----[source,c++] srand (time(NULL)); int number = rand() % 100+1; int guess = 0; cout << number << endl; //debug ---- == Do while and if statements A _do-while_ statement in C++ starts with the keyword `do`, and encloses everything that you want C++ to do in braces. You close the statemnt with the `while` keyword, followed by the condition that must be met in parentheses. ----[source,c++] do { // code here } while ( number != guess ); ---- The actual game code occurs within an *if* statement with an *else if* and *else* statements to provide the player with hints. First, I prompt the player for a guess with a *cout* statement. The *cout* function prints output onto *stdout*. Because I don't append my *cout* statement with the *endl* (endline) function, no linebreak occurs. Immediately following this *cout* statement, I tell C++ to wait for input by using the *cin* function. As you might surmise, *cin* waits for input from *stdin*. Next, the program enters the *if* control statement. If the player's guess is greater than the pseudo-random number contained in the *number* variable, then the program prints out a hint, followed by a newline character. This breaks the *if* statement, but C++ is still trapped within the do-while loop, because its condition (the `number` variable being equal to `guess`) has not yet been met. If the player's guess is less than the pseudo-random number contained in the *number* variable, then the program prints out a hint, followed by a newline character. This again breaks the *if* statement, but the program remains trapped within the do-while loop. When `guess` is found to be equal to `number` the key condition is finally met, the *else* statement is triggered, and then the do-while loop is ended. This ends the application. ----[source,c++] do { cout << "Guess a number between 1 and 100: "; cin >> guess; if ( guess > number) { cout << "Too high.\n" << endl; } else if ( guess < number ) { cout << "Too low.\n" << endl; } else { cout << "That's right!\n" << endl; exit(0); } // fi } while ( number != guess ); return 0; } // main ---- == Building the code and playing the game You can build your appication with GCC: ----[source,bash] $ g++ -o guess.bin guess.cpp ---- Run the binary to try it out: ----[source,bash] $ ./guess.bin 74 Guess a number between 1 and 100: 76 Too high. Guess a number between 1 and 100: 1 Too low. Guess a number between 1 and 100: 74 That's right! ---- Success! == C++ The C++ language is complex. Writing C++ applications for terminals can teach you a lot about data types, memory management, and code linking. Try writing a useful utility for yourself in C++ and see what you can discover!