Embarcadero Dev-C++ is free, and is a fast, portable and simple C/C++ IDE for Windows. The free version is great for beginners. If you want to develop professionally it is highly recommended you start with C++ Builder CE version. Dev-C++ can be downloaded from Embarcadero’s site, Sourceforge, or Github. The original developer is Bloodshed Software.
You can also create some small games in Dev-C++, you may need C++ Builder, VSCode, VS C++ to develop much more professional games. Every beginner should learn to code Guessing Game, which is mostly classic in most of courses. This is very good example to show how user and computer acts as in inputs and outputs with the logic in code lines. If you are new to C++, you can easily start learning C++ with this game.
First let’s describe our game,
Guessing Game: Our game is about to guessing numbers between 1 and 100. In this game the computer ( actually this is your application that you will develop) will choose a random number between 1 and 100, and the player will try to find this number in 10 steps. User should try to do as few attempts as possible. At the beginning our application will hold a random number, and it will ask for it. Each time the player enters a guess, the computer tells user whether the guess is too high, too low, or right, game will over. If user can’t find it in all steps, the game will be over and computer will print the random number that hold.
Let’s start to code,
- Create a new Project in Dev-C++
- To have a random number we will use rand() function which generates a number between
0
and RAND_MAX. RAND_MAX is maximum number that is a constant defined in <cstdlib>. We can not change constant numbers or we must modify which is not recommended.
Mathematically, in C or C++ we limit this by using modulo operator with %, for example rand()%100 means random number between 0-99. If we want to hold a number between 1-100 we must add 1 to this, so our random number will be like this,
1 |
int randomnumber = rand()%100 + 1; |
Random numbers are not really numbers, it depend on timer, so each time you run it will hold same number. In some games this is advantage to generate random maps, roads in a same random series, so you can make different levels but each level will have same shaped maps or roads etc.. To initialize random seed we must use this with srand() function as given below,
1 2 |
srand( time(0)); int randomnumber=rand()%100+1; |
So now we have a number hold.
3. Let’s keep coding with these information. We need these libraries below listed with #include to use our functions.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <ctime> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { return 0; } |
4. Now we can focus on our main program, we will code inside the main() procedure. Let’s add some variables for counting steps and random input, we can add generating a random number hold it as an integer randomnumber,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <ctime> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int i, userinput; srand( time(0)); int randomnumber=rand()%100+1; return 0; } |
5. We can print out some information texts wit cout command and let’s create our empty loop counting 10 times with { } brackets , finally let it write randomnumber at the end.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <ctime> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int i, userinput; srand( time(0)); int randomnumber=rand()%100+1; std::cout << "Welcome to Guess Game !\n"; std::cout << " I hold a number between 1 and 100\n"; std::cout << " You have 10 chance. Guess it !\n"; for(i=1; i<10; i++) { //guessing } std::cout << "The number i hold was =" << randomnumber << "!\n"; return 0; } |
At this step, you can compile by pressing F9 run your code by pressing F10, or you can use Compile and Run buttons in Dev-C++ editor. It should hold a random number, display information and then it will show the number that it hold.
6. Now we will continue to write codes inside our for() loop, go to //guessing line, delete it and add these to get input with cin command from user
1 2 |
std::cout << "Your Guess " << i << ":"; std::cin >> userinput; |
7. Now we have a user input, we need a logic to decide if it is higher or lower or equal to random number. To do this we should use if () … else if() … else() serie as below
1 2 3 |
if (userinput > randomnumber) ... else if (userinput < randomnumber) ... else ... |
8. Finally, let’s modify this logic and whole code should be as below,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include <iostream> #include <stdlib.h> #include <stdio.h> #include <cstdlib> #include <ctime> /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int main(int argc, char** argv) { int i, userinput; srand( time(0)); int randomnumber=rand()%100+1; std::cout << "Welcome to Guess Game !\n"; std::cout << "I hold a number between 1 and 100\n"; std::cout << "You have 10 chances. Guess it !\n\n"; for(i=1; i<10; i++) { std::cout << "Your Guess " << i << ": "; std::cin >> userinput; if(userinput > randomnumber) std::cout << "Lower than this\n"; else if (userinput < randomnumber) std::cout << "Higher than this\n"; else { std::cout << "Well done, you found it\n"; return 0 ; } } std::cout << "The number i hold was =" << randomnumber << "!\n"; return 0; } |
Note that last else has two lines and it exits from the program, so we put these lines in { …. } brackets to define lines of else group.
9. You can compile by pressing F9 run your code by pressing F10 or F11, or you can use Compile and Run buttons in Dev-C++ editor. It should hold a random number, display information and then it will show the number that it hold. Try to guess as few steps as possible.
In this example, there are other ways to do same game with do… while() loop or with while() loop alone. We hope this example helps you to understand some basics and logics of C++ programming. These codes are friendly with some other compilers like GNU C/C++, VS Code, Bcc or CLANG Compiler for the C++ Builder Console (VCL) application.