Monday, April 16, 2012

Back From the Dead

Hello to all my little bots who thrive within my blog. I've been sick for the past couple of weeks so my blog posts and studies had been put on hold. One word of warning if you ever wish to go to Asia, if you catch a cold it will knock the stuffing out of you. Not sure what it is about here, but every time I get sick it's always for at least a week. More than likely it'll be for a couple of weeks. The darn bug just doesn't want to die. Never had that problem back in the states, but here we have super colds.

Well, enough whining back to C++.

Last post I talked about variables and how to assign them a value. Now we are going to mess around with assigning values to the variables.

#include <iostream>
using namespace std;

int main()
    unsigned int hitPoints = 50;
    cout << "Hit Points: " << hitPoints << endl;
    //altering the value
    hitPoints = hitPoints + 100;
    cout << "Hit Points: " << hitPoints << endl;
    //shorter way to write the above line of code
    hitPoints += 100;
    cout << "Hit Points: " << hitPoints << endl;
    //Increment and Decrement Operators
    short extraLives = 1;
    cout << "Extra Lives: " << extraLives << endl;
    cout << "Extra Lives: " << extraLives << endl;
    cout << "Extra Lives: " << extraLives << endl;
    cout << "Extra Lives: " << extraLives << endl;
    extraLives = 1;
    short bonus = ++extraLives * 5;
    cout << "Extra Lives, Bonus = " << extraLives << ", " << bonus << endl;
    extraLives = 1;
    bonus = extraLives++ * 5;
    cout << "Extra Lives, bonus = " << extraLives << ", " << bonus << endl;

    return EXIT_SUCCESS;

That little piece of code will display this when ran:

Hit Points: 50
Hit Points: 150
Hit Points: 250
Extra Lives: 1
Extra Lives: 2
Extra Lives: 3
Extra Lives: 2
Extra Lives, Bonus = 2, 10
Extra Lives, Bonus = 2, 5

You can alter the value of any variable you entered by simply writing it again and giving it a new value. No need to declare it as a short int or anything. It already knows what it is.

For example: I gave my ficticious character 50 hit points with this:
short hitPoints = 50

I then altered the value with this:
hitPoints = hitPoints + 100
I told it to take the value of hitPoints and add a hundred to it. So now hitPoints is 150.

There is an even simplier way of doing that by using a combined assignment operator. In this case I used the addition operator.
hitPoints += 100

That tells the compiler to add 100 to hitPoints.
You can do this with subtraction and the rest. Here's the list:

+=    adds              example: x += 20;        same as: x = x + 20;
-=    subtracts        example: x -= 20;         same as: x = x - 20;
*=  multiplies       example: x *= 20;         same as: x = x * 20;
/=   divides           example: x /= 20;          same as: x = x / 20;
%= modulus        example: x %= 20;        same as x = x % 20;

Next I used an Increment and decrement operator.



Those add one to the variable. In my code I declared the variable extralives and gave it a value of one. Then I added one to that by using ++hitPoints. So now extraLives had a value of 2. I wrote it two different ways.

++extraLives;  this will add one to extraLives before doing anything else.
extraLives++ this will add it after.

I know it doesn't sound like much of a difference, but if you look at this:

    extraLives = 1;
    short bonus = ++extraLives * 5;
    cout << "Extra Lives, Bonus = " << extraLives << ", " << bonus << endl;
    extraLives = 1;
    bonus = extraLives++ * 5;
    cout << "Extra Lives, bonus = " << extraLives << ", " << bonus << endl;

The first bit displays:

Extra Lives, Bonus = 2, 10

and the second one displays:

Extra Lives, Bonus = 2, 5

The reason for this is that the first bit added the ++ and then multiplied extraLives by 5.

The second line added it after it multiplied it by 5. So that extraLives was 2 in both cases, but the bonus was different.

That'll do it for today. Still feeling a bit sick, but wanted to get back to my blog and coding. I hope my bots and one fan are happy again :D

As always:
Same rat channel.
Same rat time.

Monday, April 2, 2012


Hello to all the little bots that live here in my blog. Today I'll be reviewing variables and data types.

As before, I'm no expert. I'm writing this down here to help me understand what I've learned so far.

So without furthor ado I give you variables:

Variable = A portion of memory that you use to store, retrieve and manipulate data.

Each variable I create has a type which represents what kind of variable is stored. So I know of 12 different types. I'm sure there are more, but these are the ones I know of so far.

Common Types                    Values

short int                                  -32,768   to   32, 767
unsigned short int                  0   to   65,535
int                                           -2,147,483,648   to    2,147,483,647
unsigned int                           0   to  4,294,967,295
long int                                  -2,147,483,648  to  2,147,483,647
unsigned long int                   0  to  4,294,967,295
float                                        3.4E +/- 38 (seven digits)
double                                    1.7E  +/-  308  (15 digits)
long double                            1.2E  +/-  4932 (19 digits)
char                                        256 character values
bool                                        true / false

*The range of values is based on your compiler. I'm using bloodshed dev C++

char is for storing character values, int for integers, float for single-precision floating point numbers, double for double-precision floating point numbers and bool for Boolean values (true/false).

Depending on what you declare is what you can store. Here's an example using all of the types I know of:

#include <iostream>

using namespace std;

int main()
    short int level;
    float nextLevel;
    double distanceToTown;
    char rideHorse;
    bool haveHorse;
    int orcsKilled, orcsUnconscious;
    level = 2;
    nextLevel = 3.5323;
    distanceToTown = 72110.53423;
    rideHorse = 'y';
    haveHorse = true;
    orcsKilled = 3500;
    orcsUnconscious = 5400000;
    cout << "\nLevel = " << level << endl;
    cout << "Experience till next level = " << nextLevel << endl;
    cout << "Distance to the next town = " << distanceToTown << endl;
    cout << "Does the character ride a horse to town?\n" << rideHorse << endl;
    // Don't display boolean values
    cout << "How many orcs has the character killed?\n" << orcsKilled << endl;
    cout << "How many orcs has the character knocked unconsious?\n" << orcsUnconscious << endl;
    int food;
    cout << "How much food do you wish to buy? ";
    cin >> food;
    cout << "You have bought " << food << " units of food." << endl;
    typedef unsigned short int sshort;
    sshort horses = 5;
    cout << "\nYou have " << horses << " horses." << endl;

    return 0;


This little program displays:

Level = 2
Experience till next level = 3.5323
Distance to the next town = 72110.5
Does the character ride a horse to town?
How many orcs has the character killed?
How many orcs has the character knocked unconsious?
How much food do you wish to buy ? (enter any number. I chose 5) 5
You have bought 5 units of food.

You have 5 horses

For Level I used a short integer because the number would not be that high.
For Experience I used a float because I wanted to use a fractional number
Same with distance to town, but used a double instead to create a larger number
I needed a character for Does the character ride a horse to town? so I used char
For how many orcs killed/unconsious I put the integer name on the same line. I could have put many more variable names on that line if I wanted. I don't have to go to a whole new line if I am creating a bunch of the same "types" of variables

A couple of key things to remember about variables:
  • Always declare what type.
  • Use the correct type of variable. Don't try to store a character value (the 256 characters) when declaring it's an integer. 
  • If you need fractional parts then use float or double
  • You can declare the variable many different ways. You can declare it first without any value and later on give it a value.
  • OR better yet, give it a value when you declare it
  • identifier names (the name after you declared what type of variable it is) can be anything you want with a few exceptions.
    • an identifier can only contain numbers, letters and underscores
    • No C++ keywords
    • Can't start with a number
  •  Lastly, = (insert value) declares the value
Naming variables is pretty open, but try to follow a few rules. Be consistant, use descriptive names and follow traditions others have set. They don't become traditions for no reason. The way I'm handling naming is by capatilizing any second word. Some use underscores. For example:
int thisIsTheNameOfMyVariable = 5
int this_is_the_name_of_my_variable = 5

I don't like hitting the underscore. It's a pain :D

I know I could go into really detailed explanation, but it's pretty basic.

Declare, Name, Value
  • Declare your type of variable
  • Name it
  • Give it a value

Basic stuff.

int basicStuff = 5;
double notSoBasic = 5.5554;
char itIsBasic = 'y'; 
long double reallyNotBasic = 5.43432324525;
bool didYouDeclare = true;

All of those followed the rule of declare, name and value.

One last thing that is pretty cool. You can redifine how to declare a type with typedef:

typedef unsigned long ulong;

By using that I no longer have to type in unsigned long before declaring an unsigned long int. I can now just type ulong. Also you can just type short or long for short int or long int no need for typedef with those.

That's it for today. Before I go I would like to recommend again antiRTFM's utube videos and Beginning C++ Game Programming. I'm using both and am learning a lot.

Well, good night to all my little bots that thrive and live within my blog.

Same Rat-Channel.
Same Rat-Time.