Welcome to the second tutorial in my series. It is recommended that before following these instructions you read Tutorial 1.
In this tutorial we will cover;
- Creating a project
- Setting up the options
- Simple C++ Syntax and functions (console)
Part I: Creating the project
First, open Code::Blocks from the Start Menu like you were shown in the previous tutorial.
Locate the File menu at the top right and press it, scrolling over to the "new" area and then selecting "project".
You should then see this screen. Please note that most of these other buttons you will hardly ever use. They are for other C++ extensions, eg: OpenGL (a graphics engine), Ogre (a 3D graphics engine), Irrlicht (another graphics engine), GTK+ (a User Interface Engine), etc...
- Make sure you select the right option
- The submit button is highlighted on the picture to the right
Once submitted, you will see this screen. First, continue by simply clicking "Next".
On the next dialog select the C++ option. Don't worry about C however, it isn't much of a change in the language, rather lacking some functions.
Click Next and call you project HelloWorld. Click the Folder select button to find an appropriate place for your project (use C:\Projects\). While in the Folder select dialog, create a new folder on your C:\ drive and call it "Projects". Once the dialog is filled in correctly it should look like this;
If you click Next again, you will be brought to a final screen. You don't have to worry about changing any of these options. For future reference however, we will go into a little depth on the difference between Debug and Release.
- Debug: A quick compilation taking a few seconds - you want this enable on a project for quick testing
- Release: The compilation in which the compiler optimizes the code to work faster and more efficiently - can take more than a few minutes, thus not good for quick testing.
- If you have both checked you will have an option, which is the best choice, so leave it as it is.
Part II: Editing the project
After a few short moments of you pressing Finish, the project should have been created and the main window will look like the picture below. Please press the little cross highlighted by the arrow;
This will expand the treeview so you can see more folders. If you keep expanding you will finally find a file called main.cpp.
If you double click on the file it will be opened in the editor. You will also see a short snipped of code automatically generated by Code::Blocks for a "Hello World" application. Hello World is the term used in any language for your first application. It usually involved printing the words "Hello World" to the screen in some way. Take a minute to explore the GUI, but be careful not to click too much.
To quickly get an idea of what this all does, you will want to run the program. This is an important step and you will be repeating it alot later so take note. Go into the "Build" menu and press "Run";
In a small dialog it will then ask you the following. Press yes or you will not be able to test your program;
For a moment, if you glance at the bottom now, a few instructions will be printed to you. These usually mean progress as the code is being compiled into runnable machine code. Take a minute to read the output, although it will most likely make no sense to you yet;
At the end of the process, it should start up, but if it dosn't just run it again and it should open. By default the output will look like this;
By closing the window with the X located in the upper right, you can go back into the editor window now. We will briefly explain what each part of the code does. By default the entire solution should contain;
Code:
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world" << endl;
return 0;
}
The first part to look at is the first line which reads "#include <iostream>". Whenever you see an include tag it usually means that it links to another file and is asking that it be compiled with the application. In this case iostream represents a default system library called IO (Input Output) Steam (Data Flow). It's the general library used for printing and retrieving console input.
The next part reads "using namespace std". This is simply a shortcut for us. Usually when writing to the console using Iostream we would have to use an abbreviation like this;
Code:
std::cout << "This is printed text";
std::cout << "I should have put an end line, now they are on the same line" << std::endl;
By using this namespace call, we are simply making it easier on ourselves because we only have to use short functions;
Code:
cout << "How much easier?";
Now, our focus is brought to the next few lines which read;
Int main says that we are creating a function called "main" which returns an integer as a data type. At first this will seem complicated but you won't need to know too much about data types and returning for a while. The curly brackets around the end mean that the end is grouped and executed all at once. It's the difference between these next two snippets;
Code:
if (a == 1) dothis();
Code:
if (a == 1) {
dothis();
andthis();
andthis();
}
Once we are inside the main loop, this is where all the stuff really happens. The cout line is simply printing the words "Hello World" to the screen. Cout stands for Console Output. Here are some examples of using this function;
Code:
cout << "Words";
cout << 'Words';
cout << "Words" << "More Words";
cout << " ' " << ' " ';
Note all strings must be sourrounded in an identical set of quotation marks. They can either be the single lines of double lined ones. If you start a string with the double lines quotation marks you cannot end them with single lined marks and vice-verca. By combining the two you can get both single marked quotations and double marked quotations in one sentence.
Finally, the last piece reads "return 0;". You don't have to worry about this because to the human eye this actually won't effect anything. You can change this to any value, just be careful that it is under 32,000 (the uppermost memory limit for an integer).
Part III: Self written modifications
You can now modify what is printed to the screen. Not the use of the endl call. Endl stands for "End Line" and simply prints the invisible character which represents the enter key.
After all declarations in C++, the ; symbol must be used, except in some special cases. I will explain more about this in the third chapter.
Also remember that you will have to select "Build and Run" from the Build menu when running your program or you will never see changes.
Try the following snippets of code. Make sure not to delete anything else but what is actually inside the main() { ... } loop, otherwise you will find yourself with very bizarre errors;
Printing a simple one line text;
Code:
cout << "I am Fred." << endl;
return 0;
Experimenting with endl;
Code:
cout << "This has no endl";
cout << "Notice endl dosnt work inside quotes";
cout << "However..." << endl;
cout << "... it does work outside them!!" << endl;
return 0;
Using a pointer char* to store a name, then adding it to the string.
Code:
char* name;
name = "Joe";
cout << "My name is " << name << endl;
return 0;
Using several names;
Code:
char* name1;
char* name2;
name1 = "Joe";
name2 = "Fred";
cout << "My name is " << name1 << ". That over there is " << name2 << endl;
return 0;
From here, you may be inclined to save your project. If you can remember which directory you used (C:\Projects\) you should always be able to access it. Just simply press the "save all" button near the file menu (although in the toolbar). This is very important.
More of the workings of C++ will be explained in the next chapter. This chapter was written mainly to follow the "Learn and Play" philosophy. We hope you had fun with your newly trained C++ console skills.
What's in the next tutorial:
- Variable and data types
- Playing more with console input and output
Regards,
-Schyler-