C++ Tutorial #2 – Variables and Types


Variables and types are an important part of programming. Variables can store a value of a type.  Here are some examples of types:


Character (char): represents a single character. ex. ‘A’, ‘B’, & ‘C’
Numerical integer (int): represents a whole number. ex. 5, 10, 150 & 1200.
Floating-point (float): represent real values. ex. 3.14, 2.5, 1.25
Boolean (bool): represents only two states. ex. true & false
String (string): represents multiple characters or a string. Basically text. ex. “John”, “KauerGames”, &

“Game Over!”


Time to Code!


Like our last tutorial, we are going to print text on the screen again. It will be more fun this time because today we are going to be messing with variables. Let’s create a new project, or .cpp file, give it any name you want, and let’s begin!


1. Setup your code like this (you will need to include the string header because we’ll be using strings):



2. Build and run the code. If you have set it up correctly your program should look like this:


Username: John
Gender: m
Combat Level: 25
Money: 85.45
Alive?: 1


What did we just do?



In this section of code, we set up new variables and added a value to it. The first word is the type of variable we are making, the second word is a name for the variable we gave it, then we assigned it a value by using the equal sign. At the end of the line, we used a semicolon. remember to use a semicolon at the end of every line. Okay, so we wrote 5 lines of different types of variables and assigned values to it. then we wrote 5 more lines of code using “cout” to print out the text on the screen. notice how you can use the name of the variable in “cout” to print out the value it holds. Impressive right?


Note: In “cout” you can add more variables or text to the same line with  “<<” (also called the insertion operator).


Okay, maybe the program looks boring, but variables are very useful in game programming. Like I show you in this demonstration program, you can use variables, to store a character’s name, his attack level, his high score, whatever you want in your video game, and use algorithms and events to make the score go up or down. I will be explaining this in another post. In my next tutorial on C++ programming, we will be building a program that will be much more interesting.  We will make a calculator!


About Justin

Computer programmer and owner of kauergames.com.
Bookmark the permalink.

One Comment

  1. What is the use of #include in the above code

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.