We are happy to announce that today you will create your second project in the Visual Studio environment! This will be a desktop application - for a Personal Computer (PC). We will use C++ as a Programming Language.

Today, you will create the game "Guess Number". You will learn how to use "If-else If-else" constructions, create a random numbers generator, and work with the Message Boxes.

We will create the same project using different programming languages (VB.NET, C#, C++, Python, Java Script) and on different platforms (Desktop app, Web App, Mobile App, Arduino project). This is what makes the MATRIX an unique programming learning methodology.

MATRIX team

C++

PROJECT 2.3.1
Guess Number

 

For our projects, we will use the Microsoft Visual Studio application. It allows you to create projects in different programming languages and on different platforms. We guess you already have Visual Studio on your PC. If not, download and install Visual Studio on your Personal Computer from the next link:
Download Visual Studio >>>

If it is not Installed, we will need to download Extension "C++ Windows Forms for Visual Studio 2022 .NET Framework. Here are the instruction:

Open Visual Studio. Create any project. Click Extensions on the meniu panel. After Click manage Extensions. New window will open. In the search write C++ forms. Select "C++ Windows Forms for Visual Studio 2022 .NET Framework" from the list and click download.

After closing Visual Studio you will get the message. Click Modify  to install the extension.

 

Our Online Simulator will help you to make the game "Guess Number".

 

Now a little theory for our project:

 

1. Random numbers

To create a random generator we will use the next code:

srand(time(0));
int randomNum = rand() % 100+1;
textBox1->Text = Convert::ToString(randomNum);

 

Here, the first line is for getting the different numbers each time. Without this line, every time we run the code, we will get the same random numbers (it means they will not be truly random).

In the second line, we will specify random numbers ranges - from 1 to 100.
100 is for getting random numbers from 0 to 99. After adding +1 we will get random numbers from 1 to 100.

In thi third line we will put random number in text box. To put number in text box in C++, we have to convert it to string, so we are using Convert::ToString() before the variable - randomNum.

 

2. Message Box

Message Box Displays a message in a dialog box. We are using a simple Message Box in our project. Here is an example:

MessageBox.Show(" some text here ");

 

 

 

 

 

3. If - else if - else

You know how to use the If operator. else if is for adding one more condition. else means that code written after else will run if none of the above conditions is right.

Example:

If (a>b)
{
MessageBox::Show("More");
}
else if (a<b)
{
MessageBox::Show("Less");
}
else
{
MessageBox::Show("Equal");
}

 

4. System::Int32::Parse and Convert::ToString

We are usingSystem::Int32::Parse in front of textBox->Text, to tell the program text in the text boxes are numbers (Integer), so it is possible to compare them or perform other mathematical operations (We will learn about all types of variables later).

Example:

if (System::Int32::Parse(textBox1->Text) > System::Int32::Parse(textBox2->Text))
{
MessageBox::Show("More");
}
}

 

To put number in text box in C++, we have to convert it to string, so we are using Convert::ToString before the variable.

 

5. # include

The #include directive in C++ is interpreted by the preprocessor to include the header files content and can be used to include code from user-defined or compiler-based header files into a program. The directive is written at the beginning of a file to ensure that all header files are included before compiling the rest of the code.

In your project we are using the next include directives:

#include <iostream>
#include <cstdlib>
#include <ctime>

Whithout them some part of our code will not work.

 

 

Now it is time to start the simulation. Repeat the stages the simulator shows you in your Visual Studio.

 

Conclusion:

You have created the game "Guess Number" in the Visual Studio environment with the C++ programming language. At first, we worked on the program interface (visual side), after we wrote the programming code.

Now you know how to use If-else if-else construction, making Message Boxes and Random Numbers generators.