Basic Programming Structure

April 13, 2007

Assumed knowledge: Basic computer usage skills
Concepts covered: How to create a basic program

Learning to program in C++ is no easy task, it requires plenty of time and patience. To ensure that you learn the most out of C++ I strongly recommend purchasing a book to get you through as these tutorils will only get you so far but are indeed a good first step.

To get started you need a C++ compier, there are many C++ compilers out there. Any compiler should able to process our code. I recommend Dev C++ which can be obtained at BloodShed.

Open up your compiler and type the following code in:

#include
using namespace std;int main()
{
cout<<"Hello, world!";//outputs Hello, world!
cin.get();
}
Now compile and run your program. This can be done by selecting the compile and run command in your compiler. Some compilers do both at once. Compiling your program is when the code is checked to see if there are errors, if there is they will be listed somewhere in your compiler. If there are no errors an EXE file is created. Running just opens up that EXE file. The EXE file with most compilers is located in the same folder as the source code.

What should happen is that a window should open and have the words Hello, world!in it. Once you press enter the window will close.

Since you probably have no idea what all of that code means lets run though it:

#includeThis tells us that to include the input and output stream of C++ into this program. The #include part says to include whatever is in the pointy brackets. Iostream refers to the C++ input and output stream which means that we can input and output into our program.

using namespace std tells the program that we are going to be using the namespace under std. Std refers to the standard library. So this statement means that we can use the standard library in our program.

In a C++ function there always has to be at least one function. That function must be int main() . A function is defined in the following way:

type name(arguements)

We will go through types in another tutorial, the function returns a value of its type. The name is self explanatry. As for the arguments, they are the parameters that are received when the function is implemented. There are no parameters in our function so there is nothing in the brackets.

The { opens up the code block and the } closes off the code block.

Cout is the standard output stream for C++, the << operator outputs what follows it. The Hello, world! Part needs to be in brackets otherwise the compiler will think that it is a variable, which in our program there are no variables of that name. There needs to be an ; at the end of our quote to finish the line.

The // begins a comment, what follows it on the line that it was written is all a comment and doesn't effect the progrma in anyway. Comments are useful as you can use them to explain whats going on, this makes it easier for other people to share code and know whats going on.

cin.get() is similar to cout. Cin is the input stream for C++, cin.get() asks the user to press enter to continue, since there is nothing in the brackets it assumes enter.

There you have it your first C++ program!

If you enjoyed this post, then make sure you subscribe to my RSS Feed

Tags: , , , ,
Posted in Tutorials |



Related Entries




No Comments »

No comments yet.

RSS feed for comments on this post. TrackBack URL

Leave a comment

"With ordinary talent and extraordinary persevernce anything is attainable"
Thomas Fox Burton
Have you checked out my gaming personal blog yet? No! Then take a squiz: