0 Vote(s) - 0 Average 1 2 3 4 5 Tutorial [CHAPTER 2]C++ Crash Course - Variables Ich1994 Super Social Joined: Jun 2012 Posts: 279 Reputation: 14   02-04-2013, 18:47 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? Code: ```#include using namespace std; int main() {    //declare the Variable    int energy;       //define the Variable    energy = 100;    cout<<"Energy of the ship:"< using namespace std; int main() {     int a,b,c;     a=5;     b=6;     c=a+b;     cout << c << endl;          c=a-b;     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. Code: ```#include using namespace std; int main() {     int points = 10;          points = points + 20;     //"Shortcut"     points+=30;          int Timer = 0;     Timer++;     Timer++;     Timer--;          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 ? Code: ```#include using namespace std; int main() {     unsigned short test 65535;     cout << "Before overflow:" << test << endl;          test ++;          cout << "After overflow:" << test << endl;          test-=10;          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. Code: ```#include 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. If you have any suggestions for improvement, just post it here, too; The following 2 users say Thank You to Ich1994 for this post:2 users say Thank You to Ich1994 for this post

 Possibly Related Threads... Thread Author Replies Views Last Post 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 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 [Release] Health on HUD (no crash :D) xtreme2010 3 3,589 04-21-2013, 00:20 Last Post: CHRISLUVMSR MDUMP CRASH ? HOW TO FIX Raja2k11 14 3,494 04-20-2013, 05:24 Last Post: yokai134 Help - HUD element Crash Server ((( xtreme2010 4 1,952 01-20-2013, 09:22 Last Post: archit [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)