Total beginners guide to C++ - Variables and Uses

Discussion in 'Archives' started by Burden, Feb 22, 2009.

Total beginners guide to C++ - Variables and Uses
  1. Unread #1 - Feb 22, 2009 at 1:34 AM
  2. Burden
    Joined:
    Dec 30, 2008
    Posts:
    894
    Referrals:
    0
    Sythe Gold:
    0

    Burden Apprentice
    Banned

    Total beginners guide to C++ - Variables and Uses

    Total Beginners Guide to C++ variables and their uses
    By: Burden
    (This guide ssumes you have BASIC knowledge of C++, such as using namespaces, calling libraries, etc.)



    Table of contents


    • [*]Basic C++ intro
      [*]Types of variables and values
      [*]Declaring variables
      [*]Displaying variables value on the screen
      [*]Adding or subtracting from a variable
      [*]User defining the variables value
      [*]Using if/else statements.


    Basic C++ intro ​


    C++ is a programming language.
    It is a high end language, that can range from beginners to expert.
    It is a VERY popular choice for game programmers due to it's flexibility.
    It's uses for applications are very wide, and can lead to many interests with other languages.
    It comes from the C language,as the name suggests.
    It is very well supported, it can run fast, and as stated before, is flexible.
    Most games you play on the computer have C++ somewhere in them, even Jagex uses C++.


    Here is a BASIC skeleton if you wish to attempt variables without knowing the basic stuff.

    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
         VARIABLES DECLARED HERE
    
    
         CODE BLOCKS HERE
    
         return 0;
    }


    Types of Variables and values.​



    • Type: Value:
      [*]int: -2,147,483,648 - 2,147,483,648
      [*]short Int: -32,768 - 32,767
      [*]unsigned short int: 0 - 65,535
      [*]unsigned int: 0 - 4,294,967,295
      [*]long int: -2,147,483,648 - 2,147,483,648
      [*]unsigned long int: 0 - 4,294,967,295
      [*]float: 3.4E +/- 38
      [*]double: 1.7E +/- 308
      [*]long double: 1.2E - 4932
      [*]char: 256
      [*]bool: true/false


    Declaring Variables and values​


    Now that you have the types and values possible for variables, let's move onto declaring them.
    Declaring a variable is pretty much telling your computer "Hey, computer, make variableName equal number!"

    Some important things to do while declaring variables are as follows.

    • Only use numbers, letters, and underscores
    • Don't use a C++ keyword
    • You cannot start one with a number
    • Keep the variable relative to the thing it will be. (I.E. score can be "int score = 0;, but pingponglolol = 0; is plain horrid to look at when you go back and try to find what variable score was for, and for other programmers to look at at and give tips about your code.).
    • Keep them short, but still be descriptive, a long variable takes up uneeded space and time
    • Try not to start with an underscore. These usually have a special meaning among programmers.
    • Try to declare all in one general area, not in random spots in the main event.

    Now, you have read about some general rules and guidelines for variables, here's how and where to declare them.

    Usually right before, or right after the main block of code is.
    Such as:

    Code:
    int lol =1337;
    int main(){
         return 0;
    }
    Or
    Code:
    int main()
    {
         int lol=1337
         return 0;
    
    Anywhere else and it can make your code look messy, and neatness is important.

    Now, I have shown you how to declare integers (int), now some others.

    • [*]Float: float miles = 1.2;

      [*]Character: char = 'no';

      [*]Boolean: bool playAgain = false

    Displaying the value on screen.​



    As stated before, I assumed that you know some basics of C++, this includes displaying text with cout.

    This is the basic code block to display the variables value.

    Code:
    #include <iostream>
    using namespace std;
    int tutorial = 0;
    bool burdenIsLeet = false;
    
    int main()
    {
         cout << "Number of my tutorials so far: " << tutorial << endl;
         cout << "Is Burden l33t, true or false? " << burdenIsLeet << endl;
         return 0;
    }
    That being done, I'm sure you can understand how it works if you just look at the code and study it, if you know the very basics of C++ already besides vars.

    Adding or subtracting to a variables value​


    This is a key in games, and basically any programs.
    This is simply done by stating a new value for it.
    Code:
    varName = 100;
    Or, adding only one.
    Code:
    varName++
    Or adding any number to it at all.

    Code:
    varName = varName + 100;
    User defining the variable​


    This is another critical thing to have in a game.
    User defining a variable means of course, the gamer, or whatever says what the value of the variable will be.
    This is done with the simple comment
    Code:
    cin >> varName;
    So, here is what you would put for declaring your age in a program, to personalize it for you.

    Code:
    #include <iostream>
    using namespace std;
    
    int age = 0;
    
    int main()
    {  
        cout << "Please enter your age: ";
         cin >> age;
    
         cout << "Your age is " << age << ", that's cool.";
         return 0;
    }
    If you run that program, it will ask you your age, you type it in, and it will tell you your age.

    Using if/else statements with your variables​


    If statements are NEEDED, they are ESSENTIAL.

    Say you wanted to make a program that will be on a sign up form, and you wanted to make sure everyone that signs up is 13, or older.(Not mentioning how easy it is to evade this filter here :D)

    Here is what you would do.
    USE AN IF STATEMENT, OF COURSE.

    Code:
    #include <iostream>
    using namespace std;
    
    int age = 0;
    int waitYears = 0;
    
    int main()
    {
         cout << "Please enter your age: ";
         cin >> age;
    
         waitYears = 13 - age;
    
         if (age <= 13)
         {
              cout << "You are old enough to join.";
         }
    
         if (age < 13)
         {
              cout << "You are not old enough to join.\n You must be 13, you will be 13 in " <<  waitYears << "years.;
         }
    
    return 0;
    }
              
    
    If statements are statements inside of the main block of code that do what ever you tell it to, as long as a condition is met.
    "if (age > 13)
    cout >> "You can join!";"
    Means this pretty much
    "HEY, computer! If this dudes age is greater than 13, let him join in man!"

    You can have else statements, to make this shorter.

    Code:
    #include <iostream>
    using namespace std;
    
    int age = 0;
    int waitYears = 0;
    
    int main()
    {
         cout << "Please enter your age: ";
         cin >> age;
    
         waitYears = 13 - age;
    
         if (age <= 13)
         {
              cout << "You are old enough to join.";
         }
    
         else
         {
              cout << "You are not old enough to join.\n You must be 13, you will be 13 in " <<  waitYears << "years.;
         }
    
    return 0;
    }
              
    

    This may not seem like saving much time, but honestly, if you have like 30 if statements that can be made into one else statement, it helps a LOT.



    Credits​


    My friend for getting me into C++
    Random websites that gave me lessons to memorize this stuff
    Everything else is really all me.

    No pictures because, quite frankly, if you are following this tutorial/guide, and I explain it in basics, you should learn MORE by reading an interpretign the examples than looking at a picture and knowing what it does without doing anything C++ wise




    This is my 3rd guide in my quest for User Educator.

    If you liked me guide, you can check out my other guides and my application, and show your support/dis-support of my application.
    My application is HERE
     
  3. Unread #2 - Apr 1, 2009 at 6:01 AM
  4. fruit12
    Joined:
    Feb 23, 2009
    Posts:
    200
    Referrals:
    0
    Sythe Gold:
    0

    fruit12 Active Member
    Banned

    Total beginners guide to C++ - Variables and Uses

    Great guide, I might start coding c++
    TTYY
     
< free training | Daniels Free Mm Service! >

Users viewing this thread
1 guest


 
 
Adblock breaks this site