Lightbulb[C++] Tutorial 2: Hello World

Posts 1–13 of 13 · Page 1 of 1
[C++] Tutorial 2: Hello World
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;
Code:
int main { ... }
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-
Very nice stuff you got here.

Isn't unsigned char declared like:
[php]
unsigned char Variable;
[/php]

I'm not sure if initializing a char with a dereference operator makes it unsigned. Mind clearing that up for us?
Quote Originally Posted by Void View Post
Very nice stuff you got here.

Isn't unsigned char declared like:
[php]
unsigned char Variable;
[/php]

I'm not sure if initializing a char with a dereference operator makes it unsigned. Mind clearing that up for us?
Well. Arguably, you are correct, however in networking terminology unsigned means the absence of the signing bit. This means that, eg, a byte would be from 0 to 255 instead of -128 to 128 (I do believe, this may be incorrect). It also gives variables such as char[] a unlimited stack size property, in effect giving you the same output. You are however correct in saying that keyword is a correct way of declaring an unsigned variable, although is rarely, if ever, used on char[] nametypes for obvious reasons.

Regards,
-Schyler-
Quote Originally Posted by Schyler
if (a = 1)
correction: if (a == 1)

Quote Originally Posted by Schyler
unsigned char (unlimited sized variable)
unsigned char is not unlimited in size, the max size is 1 byte.
Quote Originally Posted by Retoxified View Post
correction: if (a == 1)


unsigned char is not unlimited in size, the max size is 1 byte.
Yeah, I wrote the tutorial pretty quickly and never revised it. Thanks for pointing those out Retox. (:
LOlZORZ. Sorry :P nice tut. I'm not going to use it, since i like VS better.
Im also not sure if this:
Code:
cout << 'Words';
cout << " ' " << ' " ';
will work correctly either, but I might be wrong and you corrected the other mistakes.. Also the limit isn't 32,000 for unless your using a 16 bit machine. On 32bit Windows systems an unsigned int is 2 to the 32 power which I think is a billion and something, prbly wrong, but a lot more then 32,000.

Anyway I might sticky these tutorials if it's okay with you... well and once the mistakes in the third are fixed... I might just edit it myself if or when I get time.
Quote Originally Posted by why06 View Post
Im also not sure if this:
Code:
cout << 'Words';
cout << " ' " << ' " ';
will work correctly either, but I might be wrong and you corrected the other mistakes.. Also the limit isn't 32,000 for unless your using a 16 bit machine. On 32bit Windows systems an unsigned int is 2 to the 32 power which I think is a billion and something, prbly wrong, but a lot more then 32,000.

Anyway I might sticky these tutorials if it's okay with you... well and once the mistakes in the third are fixed... I might just edit it myself if or when I get time.
I'm not sure about the quotation marks either. But I know, if you want to put double-line quotation marks in between your quotations, just put a slash. Like so:
[php]
cout << "\"";
[/php]
Output:
Code:
"
Quote Originally Posted by Void View Post
I'm not sure about the quotation marks either. But I know, if you want to put double-line quotation marks in between your quotations, just put a slash. Like so:
[php]
cout << "\"";
[/php]
Output:
Code:
"
Lol, yeah. I'm a big fan of GML, hence some of it's syntax wiggled it's way in. (:


Feel free to sticky, I don't mind.

Regards,
Schyler
I would post something like

Code:
string mystr;

cout << "Whats your name" << endl;
cin >> mystr;
cout << "Ah, your name is " << mystr;
Quote Originally Posted by Applesouce View Post
I would post something like

Code:
string mystr;

cout << "Whats your name" << endl;
cin >> mystr;
cout << "Ah, your name is " << mystr;
The type string is not globally supported.
You're kind of showing your lack of experience within this tutorial. These are completely rudimentary beginner things that should be second nature to you if you have any experience, but yet there are some discrepancies that request looking into.... The approach you've taken is very lacking.... You can explain syntax all you want but without explaining any concept they're being spoon fed on how to complete tasks rather than being guided into being able programmers...
~
Quote Originally Posted by Arhk View Post
You're kind of showing your lack of experience within this tutorial. These are completely rudimentary beginner things that should be second nature to you if you have any experience, but yet there are some discrepancies that request looking into.... The approach you've taken is very lacking.... You can explain syntax all you want but without explaining any concept they're being spoon fed on how to complete tasks rather than being guided into being able programmers...
~
I'm sorry for making you vomit through the contents of my several year old tutorial.
Posts 1–13 of 13 · Page 1 of 1
This thread is closed for replies.

Similar Threads

Tags for this Thread

None

Talk with us