• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Tutorial [CHAPTER 2]C++ Crash Course - Variables
Welcome to the second part of the C++ Crash Course.

Here you will learn the usage of Variables.

Why do you need Variables?

That's an easy question isn't?
You want to save values somewhere and store them. At some later point in your programm you want to read from its again and so on.

So how does this magic work?

using namespace std;

int main()
   //declare the Variable
   int energy;
   //define the Variable
   energy = 100;

   cout<<"Energy of the ship:"<<energy<<endl;
   return 0;
From this little snippet you can learn the following things:
  1. A Variable has a name. In this case "energy"
  2. You have to write his Datatype before his name IN the declaration
  3. The Datatype you've seen here is "int" which stands for Integer.
  4. You have to/should store a value in the Variable before you are using it(Definition)

There are Information for the Names of the variables:
  1. It can't start with a number
  2. Variablenames are Case-Sensitive

And how do I calculate with them?

It's like in normal school. You can add(+),subtract(-),multiply(*),devide(/) and of course can use the brackets () and the equal symbol (=).
The same rules you've learned in school applies to C++. Multiply and devide will be performed/calculated before the addition and substraction.

#include <iostream>
using namespace std;

int main()
    int a,b,c;
    cout << c << endl;
    cout << c << endl;
    cout << a*b << endl;
    cout << a/b << endl;
    return 0;

If you compile and run the above Code-Snippet you'll maybe wonder why a/b is 0.
Don't forget that the Datatype is an Interger. The decimal will be cut and the 0.8333 will be a 0. I will talk about other Datatypes later.

#include <iostream>
using namespace std;

int main()
    int points = 10;
    points = points + 20;

    int Timer = 0;
    return 0;
C++ allows to use the operator += to shorten the long version of "name = name + something". analogical you can use -= *= and /= too.
There is one more operator which is used frequently. "++" increments the variable by 1 and "--" decrements it.

That's nice, but what if i want to use decimal numbers?

Therefor C++ provides some Datatypes:
  • char - Mostly used to save Characters. The size is 1Byte
  • int - Only capable to store Integer values. negative or positive. Normally 4 or 8 Bytes long
  • shot - Same is Integer but the size is 2 Bytes long
  • long - Just like int but you can store largers Numbers in here - 8 Bytes
  • float - Here you can store decimal Numbers
  • double - just like float but more precise

Can i store the number 290578230947908567290384759234756 ?
#include <iostream>
using namespace std;

int main()
    unsigned short test 65535;
    cout << "Before overflow:" << test << endl;
    test ++;
    cout << "After overflow:" << test << endl;
    cout << "And another one:" << test << endl;
    return 0;
First i have to explain you what "unsigned" means. It is what it says. It won't store negative values anymore, therefore you have 1 bit more of space for your number.

What happens in this code? Why is 65535 + 1 = 0 ?
The Computer stores the values in binary format. Short is 2 bytes long. Every byte can represent 256 Values. 256*256 = 65536. So it can represent 65536 Values -> 0 to 65535.
Example with 1 Byte:
255 in binary: 1111 1111
256 in binary: 1 0000 0000
Result: 0000 0000

So everything is cool and i can print the variables on the console. But how can i input values into the variables?
Do you remeber, cout is a synonym for ©onsole (out).
The function you need is also easy to remember. "cin" for ©onsole (in).

This function is as easy to use as cout.
#include <iostream>
using namespace std;

int main()
    int round1,round2;
    int totalPoints=0;
    cout << "Enter the Points for the first Round: ";
    cin >> round1;
    cout << "Enter the Points for the second Round: ";
    cin >> round2;
    totalPoints = round1 + round2;
    cout << "The total Points are: " << totalPoints;
    return 0;
See how the Shift Operator "<< or >>" shows in the direction where the Variable is written to.
! You can only use << for cout and >> for cin.

Please report Typo's and grammar mistakes here. Wink
If you have any suggestions for improvement, just post it here, too;
[Image: compiling.png][Image: aLKA8og_460sa.gif]
[-] The following 2 users say Thank You to Ich1994 for this post:
  • aosma8, Nekochan

Possibly Related Threads...
Thread Author Replies Views Last Post
Exclamation Help cmdlist, dvarlist server crash Nerus 17 4,966 11-09-2013, 23:54
Last Post: Nerus
  Making a harrier crash into the map? akillj 6 1,740 06-09-2013, 05:57
Last Post: akillj
Its My Birthday! Happy birthday, Crash! Party Bot 0 670 06-08-2013, 00:00
Last Post: Party Bot
  [Tutorial] How to use variables as long as server runs Justin 4 1,911 05-04-2013, 20:54
Last Post: Nekochan
Brick [Release] Health on HUD (no crash :D) xtreme2010 3 3,589 04-21-2013, 00:20
  MDUMP CRASH ? HOW TO FIX Raja2k11 14 3,494 04-20-2013, 05:24
Last Post: yokai134
Sad Help - HUD element Crash Server ((( xtreme2010 4 1,952 01-20-2013, 09:22
Last Post: archit
Information [Tutorial] [CHAPTER 1]C++ Crash Course - Getting Started Ich1994 6 1,693 01-04-2013, 16:23
Last Post: Ich1994
  Help Maximum Parent Script Variables DidUknowiPwn 11 3,315 11-21-2012, 19:30
Last Post: DidUknowiPwn
  Help Server Crash Pyro2142 1 1,045 06-28-2012, 03:41
Last Post: DidUknowiPwn

Forum Jump:

Users browsing this thread: 1 Guest(s)