Saturday, March 31, 2012

Arithmetic

Hello to all the bots that live in my little blog and hello to Petteri my second nonbot to post a comment.

Just a quick recap:

  • I'm learning C++ because of an event that took place in my life which made me look back at the past ten years. That made me think of what I could have done in those ten years.
  • I've been at this for a little over a month.
  • I'm using this blog as a personal/public journal on what I've learned so far. My hope is that friendly netizens will pop in from time to time to offer a word of advice or just shoot the breeze about coding.
  • My ultimate goal is to not give up. This blog worked for helping me to stay focused with finishing a game. I'm hoping it will help keep me on track with learning C++ and in ten years time I can look back at these first blog posts and think what a NOOB I was.
  • Lastly, this blog will help me review what I've learned.

Now onto the source code: Arithmetic

#include <iostream>
using namespace std;

int main()
{
        cout << "5 + 5 = " << 5 + 5 << endl;
        cout << "5 - 5 = " << 5 - 5 << endl;
        cout << "5 * 5 = " << 5 * 5 << endl;
        cout << "5 / 5 = " << 5 / 5 << endl;


       cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;
       cout << "5 / 6 = " << 5 / 6 << endl;


        cout << "5 + 5 * 5 = " << 5 + 5 * 5 << endl;
        cout << "(5+5) * 5 = " << (5 + 5) * 5 << endl;


        cout << "5 % 5 = " << 5 % 5 << endl;


        system("PAUSE");
        return 0;
}

This bit of source code will display:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1
5.0 / 6.0 = 0.833333
5 / 6 = 0
5 + 5 * 5 = 30
(5 + 5) * 5 = 50
5 % 5 = 0

First thing to note is that there are no new lines between any of the arithmetic problems even though I put spaces between some of them. Example:

        cout << "5 + 5 = " << 5 + 5 << endl;
        cout << "5 - 5 = " << 5 - 5 << endl;
        cout << "5 * 5 = " << 5 * 5 << endl;
        cout << "5 / 5 = " << 5 / 5 << endl;


       cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;

       cout << "5 / 6 = " << 5 / 6 << endl;


Displays:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1
5.0 / 6.0 = 0.833333
5 / 6 = 0

That might not seem like a big deal, but if you don't tell the compiler that you want a space then it will not create any new spaces. One way to put a space between the lines would be:

         cout << "5 / 5 = " << 5 / 5 << "\n" << endl;

This would display:

5 + 5 = 10
5 - 5 = 0
5 * 5 = 25
5 / 5 = 1

5.0 / 6.0 = 0.833333
.
.
.


The reason for this is that \n character.

\n = new line
Couple of things to remember for \n is that it needed to be put between the " ". If you don't then you will get a compiler error. Also, it will go to a new line wherever you put it. For example:

       cout << "5 / 5 \n = " << 5 /5 << "\n" << endl;

      cout << "5.0 / 6.0 = " << 5.0 / 6.0 << endl;

Displays:

5 / 5
 = 1

5.0 / 6.0 = 0.833333

I created 3 new lines in that one line of code. One after 5/5, another after the sum of 5/5 and another with endl;
It might seem like a no brainier, but it takes a while to get used to. The compiler does not care what you intend to do, it only does exactly what you say to do. I've created a few programs where the text was messed up because I forgot to add \n.

Arithmetic expressions:

The compiler also does not care what you type in between the quotes. I could have typed "This is a sandwich  = " << 5 / 5 << endl; and it would have displayed

This is a sandwich = 1

Now after the quotes I had another funnel << and an arithmetic problem. You just need to type the math problem. You don't need any quotes. As a matter of fact you can't use them. Just type the problem in and the compiler will solve it for you and display it. The arithmetic characters are:

+  add
-  subtract
* multiply
/ divide
% modulus

Using those will express a value. Example: The expression 5 + 5 expresses the value 10.

The operators themselves are simple enough .Nothing surprising there except for %
That's a modulus. A modulus gives you the remainder when dividing two numbers. For example:

5 % 5 has a remainder of 0 so the answer is 0
9 % 5 has a remainder of 4 so the answer is 4

There are all kinds of ways I don't understand the modulus and how to use it so I'm not going to pretend to. I do know that when creating random numbers I need to use it, but random is a whole other type of beast. I'll blog about that in a later blog.

What I found most interesting about how C++ does math problems is that if you do not put a ".0" then it will not return any number after the decimal point. It will just return an integer. You need a .0 to get any kind of floating point. A floating point is a number with a fractional part. More on floats and integers later.

In the last example I am adding and multiplying the same numbers but in different orders. Just remember these order of operations

multiplication, division and modulus have equal precedence. They are evaluated first regardless of their position. In other words, they are evaluated before addition and subtraction.

To get around this you can use parentheses. Example (5 + 5) * 5 equals 50 because you first add 5 and 5. Then multiply it. Without the parenthesis you would multiply 5 and 5 and then add 5 which would be 30.

That does it for today.

Recap of what was learned:
  • \n creates a new line
  • Type the arithmetic problem out without quotes
  • + - * / % are the arithmetic operators you use to get an expression
  • Expression is something that evaluates to a single value.
  • To get a floating point expression you need to add .o to the number
  • Order of operations. * / % expresses before + -
  • Use parenthesis to get lower precedence operators to express a value first. Keep this in mind because it will become very important later on when learning more about C++
To the few netizen travelers and all the little bots that live in the void that is my blog, see you next time.

Same Rat-Time.
Same Rat-Channel

 

Friday, March 30, 2012

The Basic Commands

Here's more of what I've learned so far. The basic stuff cont (not basic as in BASIC :))

Whitespace - The compiler mostly ignores blank spaces aka whitespace. Whitespace is used mostly for us humans so we can read the code easier.

#include - This is a preprocessor directive. It tells the program what to include in your program. A file that you include in the program is called a header file. Examples of some header files and how to write them:

#include <iostream>
#include <string>
#include <vector>

// commenting - If you want to comment on some piece of code to make it easier for you or others to understand what it does then type // for a comment that is only one line or /* */ for multiple lined comments. Example:

// This is my comment
#include <iostream>
.
.
.
or

/* This is
my multiple
lined comment.*/


#include <iostream>
.
.
.


Functions - A function is a group of programming code that does some work and can return a value. More on functions later, but for now all you need to know is that you need to have a main() function in every program. Example:


#include <iostream>


using namespace std; 


main()
{}


I'm probably repeating myself but get used to that. You'll be repeating yourself in the code many many many times or at least I am. To really understand anything I have to play around with it. Trying different things and seeing if it works. Now onto more basic things.


cout - I didn't mention before that cout is a string literal which means it's literally the characters between the quotes.


cout is an object from the iostream (header file we included in the first program).


<<  - The name for this is output operator. Like I said before, it takes whatever is on the open ended side and puts it in the closed end side.


If I wasn't using "using namespace std;" then I would have to attach a prefix to the cout string literal. For example without using namespace std;


#include <iostream>


main()
{
       std::cout << "Prefix example.";
       return 0;
}

That std is a namespace which tells the compiler where to look for "cout". In this case the compiler looks for "cout" in the standard library. It's like an area code. Without that area code the compiler wouldn't know where to look for cout.

That is where "using namespace std;" comes into play. This little line of code loads the standard library into the program so you don't have to type an area code every time you want to use cout or anything else from the standard library. I'm sure there are reasons to use a prefix with every line of code, but for what I want to accomplish I think for now it would be much easier if I don't have to type that in every time I want to write an object from one of the libraries. I would have to do that with every single object I used. Example:

#include <iostream>


main()
{
      std::cout << "Another example." << std::endl;
     return 0;
}

Since both cout and endl are in the standard library I would have to tell the compiler where both of them were.

This of libraries like this:

There's the standard library. Within that is iostream. Within that is cout.

 ; - I mentioned this before, but I'll get more techincal this time around.

All statements must end in a semicolon. cout << "blah blah blah" << endl; is  a statement so it needs a semicolon. Simple as that. Just like most sentences need a period or some other form of closing punctuation.

return 0;

This will be explained more when dealing with functions, but for now just think of this as the off button.

So let's wrap up so I don't keep repeating myself next blog entry.

We've learned about:

whitespace
#include
commenting
functions (tiny bit)
cout (a string literal)
headers
libraries
;
return 0

I apologize in advance if you really are trying to learn something about C++ by reading this. I'm still learning myself and my thought process is all over the place. I'll try to keep things more organized from now on.

Well to all the little bots that live in the void that is my blog, see you next time.
Same rat channel.
Same rat time.....maybe :D

Thursday, March 29, 2012

Learn C++

Hello to the endless void that is my blog :D

Just a quick update. I did pass Avernum which was the goal of creating this blog in the first place, but I failed to update the blog. I took all kinds of pictures too, however when your computer crashes and you lose all your pictures then it's a bit hard to redo it all. So it's a win for me and a loss for my blog.

Once again I am going to treat this blog as my own personal/public diary. This time my goal is a bit harder than passing a game. My goal is to learn C++. The reason for this is that it's been a dream of mine since I was a kid. About a month and a half ago I took a long look at what I've done in the past 10 years and it made me think of what I could have done in those 10 years. While those years weren't exactly wasted, there was so much more I could have done and wanted to do. So now it's time to give myself a 10 year goal of learning C++ and make a game or two, hopefully more than two in ten years time, but I'll keep my expectations low.

To accomplish this goal I have bought a few books, bug a couple of my (mostly just one) programmer friends and found some great sites on the internet. I want to thank Jay Barnson of Rampant Coyote for blogging about all of his experiences in the game development world and for helping me out with the most basic of C++ code. Jay is a really nice guy who goes out of his way to help people and makes some damn fine games in the process. If you've never heard of him then maybe you've heard of Twisted Metal. He worked on that series and blogs frequently about the good old days which are always a good read.

His latest game, Frayed Knights, is superb. I was lucky enough to beta test the game and loved every minute of it. If you've never heard of it, it's an RPG with turnbased combat. The writing reminds me of Terry Pratchet. Not quite so demented at times, but it's pretty darn close. I've played so many games that I hardly ever have any game that actually makes me laugh out loud. This one did. I highly recommend anyone who reads this to give the demo a try. If you like old school RPGs then more than likely you will love this one, unless you're one of those people who has to have everything so damn dramatic all the time. Then you probably won't get the humor. If I had to compare the game to any others it would be like Quest for Glory mixed in with Wizardry.


Enough of the background, let's get to what I've actually learned in a month and a half. We'll I've gone from knowing absolutely nothing about C++ to understanding a few key concepts. I've been using a mix of books, help from Jay and youtube videos. One in particular, antiRTFM. His videos are excellent. He goes very slowly over every new concept learned and gives many examples of what new function does. I've been using his videos (and Jay's help) more and more.

So here's my first program:

#include <iostream>

using namespace std;

int main()
{
       cout << "Hello World!" << endl;
      return 0;
}

That little program is the usual first program. It displays "Hello World" on the screen.

In that program you have a few key concepts I needed to learn.
First are headers.

Headers are what is loaded before the program runs. Basically in the C++ libraries there are all kinds of commands/info you can use that has already been created by other programmers, but you need to tell the program you will be using them before you can use them.

main() - Every program needs a main function. Without it your program won't run.

{} block of code - A block is anything between those curly braces {} Later on I learned about nesting and scopes, but for now just think of that as an opening and closing of a letter. You need "Dear ________," and "Sincerely," in a letter. Same thing in C++. You need a { and a }.

cout - cout is console output. Everything in between the " " will be displayed on the screen. It doesn't matter what it is as long as it's between " ".

endl - end of line. I just think of endl as the return button. There is also /n which will also create a new line, but most of the books and videos I see use endl, so I'll use it. Also there is a difference between the two, but right now it's not that important for me to know the difference. Later on with more complex code, I'm sure I'll need to review when to use /n and endl.

; - That semicolon goes on the end of most lines of code in your program. Certain lines of code don't use it, but from my experience most of them do. Example: For (;;) loop doesn't use a semicolon at the end. A cout statement needs it.


<< and >> - I don't remember the name to those, but I think of them as a funnel. Anything on the open end gets funneled into the small end.

I'll end this blog entry here for now. I have learned a lot more than just those commands, but I'll need to review the actual wording and what they do before I blog about them. Like that for loop has many conditions within (), but while I know what they are, I don't remember the actual wording for them.

Hopefully in ten years I can look back at this and think what a newb I was. I would love to have that feeling.


Anyway, have a great day to the few people/bots who came to my blog by accident.