(adsbygoogle = window.adsbygoogle || []).push({});
I am writing this guide to try and help people who are starting off with learning C++ programming I will avoid going into too much detail so that even people
with no prior experience can understand this
Although this guide is mainly aimed at new users of C++ like myself it is also useful for those who need a little update on the new features the language
has acquired from the latest standards.
The program I will be using is
You must login or register to view this content. but this should work on any C++ Compiler
All Of The Tutorials In This Guide Will Be For "Console Applications" Most (If Not All) Compilers Support This.
If you are using Code::Blocks do this to start a console application
1. Open Code::Blocks (you might need to select a compiler just chose the default one)
2. Select "Create A New Project"
3. Select "Console Application"
4. Select "C++" And Hit Next
5. Select A Project Title And A Folder To Create It In And Hit Next
6. Just Hit "Finish" At This Screen
7. On The Left Of The Screen Open The "Sources" Branch And Then Open "Main.CCP"
8. And Delete Everything In There
9. This Is Where You Will Write Your Code
LOG
08-03-11 09.30 – I started to this guide
10-03-11 09.25 - Finished Hello World Program And Published To NGU
19-03-11 19.05 - Added Quiz Page And Question 1
[multipage=Basics/Structure Of A Program]
The best way to start learning a programming language is probably to actually write a program (I advise actually typing out each of these individual lines
rather than copy and pasting them) and guess what program we will be writing YEP your right it’s the “Hello World” one
Line 1.
Originally posted by another user
#include< iostream>
lines that begin with the # sign are not regular code lines with expressions but are indications for the compilers pre-processor the line “#include<iostream.” tells the pre-processor to include the iostream standard file. This specific file (iostream) includes the declarations of the basic standard input/output library in C++
Line 2.
Originally posted by another user
using namespace std;
All the elements of the standard C++ library are declared within what is called a namespace. This line is very frequent in C++ programs that use the standard library
Line 3.
Originally posted by another user
int main ()
This line corresponds to the beginning of the definition of the main function. The main function is the point by where all C++ programs start their execution, independently of its location within the source code. It does not matter whether there are other functions with other names defined before or after it - the instructions contained within this function's definition will always be the first ones to be executed in any C++ program. For that same reason, it is essential that all C++ programs have a main function.
The Next Set Of Lines MUST Be Included In Braces ({}) You Will Get A Better Idea Of What I Mean When We Have The Program Written Out In Full
Line 4.
Originally posted by another user
cout << "Hello world!";
This line is a C++ statement. A statement is a simple or compound expression that can actually produce some effect. In fact, this statement performs the only action that generates a visible effect in our first program cout is the name of the standard output stream in C++, and the meaning of the entire statement is to insert a sequence of characters (in this case the Hello World sequence of characters) into the standard output stream.
Line 5.
Originally posted by another user
cin.ignore();
I had to add this line myself and this line will stop your program ending automatically when you launch your .exe with some compilers this is not needed but with mine it is all it does is basically require you to hit enter before ending the program
Line 5(Alternative).
Originally posted by another user
cin.get();
This line is basically an alternative way to stop the program ending itself automatically.
Line 6(Optional).
Originally posted by another user
// This is My First C++ Program
line's beginning with a "//" are known as comment line's and are not read by the compiler but are very useful in large programs
If you write all of this out you will end up with a program like this
Originally posted by another user
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
cin.get();
// This is My First C++ Program
}
Build and run your program
Press "F9" in Code::Blocks
Other possible layouts for the same program
In C++ the same type of program can be written in many different ways bellow are a few other choices
Originally posted by another user
#include <iostream> using namespace std; int main() { cout << "Hello world!"; cin.get(); // This is My First C++ Program}
this layout is VERY unorganised but it is the EXACT same as the others just all on 1 line
Originally posted by another user
// This is My First C++ Program
#include <iostream>
using namespace std;
int main()
{
cout << "Hello world!";
cin.get();
}
because the comment line's aren't read by the compiler it doesn't matter where they go in the program
[Multipage=Quiz]
on this page I will put up some questions who ever answers the questions via private message will get + (or - if they want) rep please only answer via private message so the answer isn't given away
Q1. What is missing from this piece of C++ code
Question Posted On: 19-03-11 19.05
Question Answered On: N/A
1st Answered By: N/A
Hint
Its near the end of the code
Originally posted by another user
#include <iostream> using namespace std; int main() { cout << "Hello world!"; cin.get(); // This is My First C++ Program