Monthly Archives: January 2012

C++ || Hello World!

This page will consist of creating the typical “hello world!” application. First, you will need to create a blank project. I am using Visual C++, but any compiler will do. When you have everything loaded up, you will start your code off by entering the headers like so into your .cpp file.

The first line’s name stands for Input/Output Stream, and overall, these header files are used to handle the input/output process within your program.

Next, you are going to add the main function. This will generally look something like this

Adding the line of “return 0” is an exit code, and will tell the computer that your program executed without errors.

We are halfway finished, now all we have to do is say hello to the world! You will do that by adding this line of code to your program, which basically outputs data to the screen

Notice you need to add double quotes around the text you wish to output to the screen. Also, I added a line break, also knowns as “endl;” into my function. This basically is like the <_br> tag in HTML.

So when you add everything together, your full code will look something like this

And there you have it. After you compile the above code (by pressing CTRL + F5 in Visual C++), you should get this as your output

Hello World

C++ || Creating a Blank Project in Visual C++

As was stated in my previous post, all of the code that is provided on this site was created using Microsoft Visual C++. I remember when I took my first programming class, I had no idea how to use the software, there are alot of options provided in the program. So I am assuming, if you are still reading this page, you do not know how to use VC++ very well right now either. So this page will demonstrate how to create a blank (.cpp) project in a step by step fashion.

First you (obviously) need to open up the program. I am currently running Visual C++ 2010, so my start screen looks as follows.

(on all images, click to enlarge)

After you got the program running, next in the upper left hand corner, hit:

File > New > Project

A new dialog box should now appear as so:

As you can see from the image above, “Win32 Console Application” is highlighted. To create console based applications, you MUST select that option.

After you select “Win32 Console Application” from the dialog box, next choose a name for your application. You can name this anything you want.

After you create a name for your project, hit the “OK” button. The software will then create a project for you, aswell as a folder located in the directory listed below.

Documents > Visual Studio 2010 > Projects 

Now a new dialog box should appear, which “welcomes” you to the program. In order to create a blank project, you MUST hit the “Next” button. Do not hit “Finished,” or the program will fill your project with garbage (unnecessary) code. After hitting the “Next” button, yet another dialog box will appear, thankfully this is the last one.

You should now see an option that says “Empty Project,” which should, like it says, create an empty project for you. Make sure all your settings looks exactly like this before hitting the “Finish” button

In order to add a blank .cpp file to your document, the following steps will be very similar to the previous ones. On the left hand side of the program, your going to right click on the “Source Files” folder, and hit:

Right Click on “Source Files” Folder, hit: Add > New Item

In doing that, the program will prompt you to select the type of item you want to add.

Select that you want to add a .cpp file, give your source file a name, then finally, select the “Add” button.

Now you should see the dialog box disappear, and you should now be free to code anything you want, in a blank document!

(Quick Note: To compile your code, hit CTRL + F5)

C++ || Which Compiler To Use?

A common question one may wonder is, which compiler should I use? There are a few choices, and your choice should probably be based on what you intend to write with it.

If you intend to program on Windows, the path of least resistance is to download one of the Visual Studio Express Editions. This provides you with a straightforward IDE with (obviously) good integration with the Windows platform. Particularly the visual debugger is something many find of great value while learning a language. Click here to download the latest version from Microsoft’s website (free).

Unlike the VC++ experience,  there is no standard GNU IDE, although there are many command line compilers out there that suits many different users needs. As far as IDE’s go, Eclipse is a popular alternative, as is Codeblocks. Both of these will also give you a visual debugger and a build environment.

Ultimately, my opinion is, if you’re running a Windows computer, go with Visual C++. Then when you’re more comfortable with what you’re doing – explore the alternatives.

Personally, I use Visual C++, and all the code that is posted on this site was created using that IDE