[C++] Tutorials: #1

🚫
Exiled
Joined
Aug 7, 2006
Messages
1,756
Reaction score
3
Getting Started:

By the end of this tutorial you would have learnt how to output text,

Intro:

I've always been sort of dissapointed with RaGEZONE's Coders section. It's always been full of VB-wh0res and web developers (although, WDs aren't bad). I've always wanted RaGEZONE's coders section to become more Desktop-programming orientated (Not inc VB). Although I think only 5-10% of this section consists of this.
So I'm here to provide people with a series of C++ tutorials (How many, is depending on the comments I recive: Good/Bad).
So here's Lesson #1:

The first you're going to need to begin your C++ adventure is a descent compiler. There are many compilers about... some of which, barely pass as a compiler.

Although there are many C/C++ compilers about, there are two in mind that are considered the best (better userability/more user-friendly, better compiler and better debugger).
Personally I'd say Microsoft's Visual C++ Compiler and IDE has a better debugger, I feel Bloodshed's Dev-C++ IDE and Compiler is more user-friendly, and easier to navigate.
But don't take my word for it, try it out for yourself:


(Windows Compatible)
Microsoft Visual C++:
Overview: Download details: Visual C++ 2005 Redistributable Package (x86)
Download: http://www.microsoft.com/downloads/...-3266-4157-a2ec-5174867be706/vcredist_x86.exe

(Windows Compatible) (Reccomended)
Bloodshed's Dev-C++:
Overview: Bloodshed Software - Dev-C++
Download: SourceForge.net: Downloading ...

Other: (Reccomended by Daevius)
Code :: Blocks:
Download: SourceForge.net: Downloading ...

Okay, so I'm going to be writing a tutorial as if you were using Dev-C++ v5.0, although if you have any other version of Dev-C++ or even a different compiler, the simple things like Compile and Run are pretty much simple.

So, lesson #1 is going to be teaching you how to compile your first program, and explaining data types.
So I'm going to need you to install the provided downloads given to you, install the programs and then double click the exe to launch the program.
So after you have launched the program, you're going to want to create a console application (the next few tutorials we will be using console application).
So go to File > New > Project... and then select Console Application, and name it 'MyProject' or anything you want (It's just a name), but it's best to keep a directory, for easy access and organisation. Once you've given a name to your project, make sure that 'C++ Project' is selected. Once you have done all of this, click 'Ok'.

Now, you should have a window with the following code in it:
Code:
[B][COLOR="Red"]#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
[/COLOR][/B]
...or some similar code.

What I want you to do is highlight it all and delete, so your project is left empty.

Okay, so after that's done, I want you to take a look at this code:
Code:
[COLOR="Red"][B]#include <iostream>
using namespace std;[/B]
[b]int main()
{
cout << "Hello There RaGEZONE\n"; [/B][/COLOR] [COLOR="SeaGreen"]// Outputs the sentence "Hello There RaGEZONE")[/COLOR]
[B][color="red"]cin.get();[/color]   [color="seagreen"]//  Requests user to hit Enter key to end application[/color]
[color="red"]}[/color][/b]

Anything familiar about the above code? If not, no worries at all :) This is the reason I'm here, to teach you what this means and how to use it :)

That's start with #include <iostream>.
#include <iostream> is something called a preprocessor directive that requests code from that header file to put into our application.
By including a header file, it provides you with the use of certain functions, such as cout.


The next line of code is: using namespace std;
using namespace std; is similar to that of <iostream>. Basically, this line of code is something called a standard library. Using this line, it allows you to use certain functions such as cout. (See the similarities?). Also, notice the semi-colon ( ; ) on the end of the the line of code. This is apart of C++'s syntax and is used for most commands.



Now the next line is int main ().
This is the beginning of the main functions. It's where the C++ program's start their execution (depending on the location of the code).
int is for when the fuction returns an integer.
The braces ( { } ) bascially defines the beginning and the end of the execution.
Everything after { is what will be executed, everything before } is what has been executed.

Note: When I say "Executed" it basically means performed/done/run.


cout << "Hello There RaGEZONE\n";
Now what this is doing is outputting the phrase "Hello There RaGEZONE"
Think of the statement cout as output.
cout is declared in the iostream standard file within the std namespace, this is the reason why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. (Every line of code is done for a reason). The bracket things << are insertion indicators, indicating what our output is going to be.
The \n means newline. What it does is seperate output on the screen, so it doesn't look disorganised:
Example:
Code:
[COLOR="Red"]cout << "Hello Ragezone";
cout << "I'm Awesome";[/COLOR]
This would output as:
Output said:
Hello RagezoneI'm Awesome

Now, if you wanted a space between Ragezone and I'm, you would have to make sure you left a space in one of them:
Code:
[COLOR="Red"]cout << "Hello Ragezone ";
cout << "I'm Awesome";[/COLOR]
or
Code:
[COLOR="Red"]cout << "Hello Ragezone";
cout << " I'm Awesome";[/COLOR]
Or if you wanted it to be one line of code:
Code:
[COLOR="Red"]cout << "Hello Ragezone\nI'm awesome";[/COLOR]
Output:
Output said:
Hello Ragezone I'm awesome
Note: Notice how the \n (newline) is ALWAYS within the double quotation marks? This is a Must!

Note: C++ is a case-sensitive language, so everything has to be the way it's supposed to be:
Cout = wrong
cout = right

//
This is simply to mark line notes. They don't affect the application in anyway, they're only used for viewers of the code.
/* this is
a
blocknote*/

A blocknote is a... well, a note formatted as a block. This is used for full, descriptive explenations of a piece of code.

cin.get();
(Notice the ; ? It's been used throughout this application, remember what I said? It's used on functions).
Without placing the cin.get(); , the application window will instantaneously open and close. By placing cin.get(); at the end (before the end bracket), it means the application needs the user to press enter for the application to close.

After you have typed all of this code in, and know what everything is, try mesing about with the code a bit. Insert \n with new phrases. Learning C++ is all about exploring. I'm going to give you a guideline but you will have to explore solo a bit if you want to fully understand C++'s nature :)

And for the end part:
After you are happy with your codes, go to Execute > Compile & Run name it to whatever you want and there you have it! Your first C++ application.
This is the end of C++ Tutorial #1
I will be writing C++ Tutorial someime during the 20th of August.

What will be in Tutorial #2?:

You will be learning about data types, becoming more familiar with console applications and functions within. I will teach you how to use declare and define, how to manipulate integers and will result in teaching you on how to make a calculator

C&C Would be greatly appreciateed :)

Any questions about this tutorial or future tutorials are welcomed and will be answered ASAP :)

Credits: Me...
 
Last edited:
Re: C++ Tutorials: #1

Ahh no problem bud :) Glad I inspired someone.

And yeah, any questions/queries, don't hesitate to ask.

I'm not sure what I'm going to do for Lesson #4 (I already know what #3 is).
So, any suggestions? What would you like to know about C++? Anything you guys would like to make?
 
Re: C++ Tutorials: #1

Now should teach em how to use cin and cases =] and maybe whiles.
might be a little rusty but should be correct
example:
Code:
int main()
{
	static int selection;
	while(selection != 2)
	{
		cout << "Make your choice\n";
		cout << "1: Test/n/n";
		cout << "->";
		cin >> selection;

		switch(selection);
		{
		case 1:
			cout << "/n/n/n Good you used a case";
			break;
		}
	}
}
 
Re: C++ Tutorials: #1

Now should teach em how to use cin and cases =] and maybe whiles.
might be a little rusty but should be correct
example:
Code:
int main()
{
	static int selection;
	while(selection != 2)
	{
		cout << "Make your choice\n";
		cout << "1: Test/n/n";
		cout << "->";
		cin >> selection;

		switch(selection);
		{
		case 1:
			cout << "/n/n/n Good you used a case";
			break;
		}
	}
}

As I stated, Tutorial #2 will be teaching people how to make a calculator... and what do you need to do with a calculator? Thaaat's right; input :p

Plus, if you'd like to contribute in some way, try to give an explaination for the pieces of code. To a beginner, what you have given would be 90% 'WTF', unless you explained it.
 
Re: C++ Tutorials: #1

Nice guide :) But how is the program suppose to come up? IN the Dev-C++ Program or how is it suppose to run

Once you've compiled your code, it should come up in Window's command prompt:

Step 1:
KyleEnos - [C++] Tutorials: #1 - RaGEZONE Forums


Step 2:
KyleEnos - [C++] Tutorials: #1 - RaGEZONE Forums


Result:
KyleEnos - [C++] Tutorials: #1 - RaGEZONE Forums
 
Re: C++ Tutorials: #1

Thx for the guide KyleEnos. Its very good and detailed! :) I already know Java and wanted to learn one more language. Which you think is best C, C++ or C# and why?
 
Re: C++ Tutorials: #1

Getting Started:

By the end of this tutorial you would have learnt how to output text,

Intro:

I've always been sort of dissapointed with RaGEZONE's Coders section. It's always been full of VB-wh0res and web developers (although, WDs aren't bad). I've always wanted RaGEZONE's coders section to become more Desktop-programming orientated (Not inc VB). Although I think only 5-10% of this section consists of this.
So I'm here to provide people with a series of C++ tutorials (How many, is depending on the comments I recive: Good/Bad).
So here's Lesson #1:

The first you're going to need to begin your C++ adventure is a descent compiler. There are many compilers about... some of which, barely pass as a compiler.

Although there are many C/C++ compilers about, there are two in mind that are considered the best (better userability/more user-friendly, better compiler and better debugger).
Personally I'd say Microsoft's Visual C++ Compiler and IDE has a better debugger, I feel Bloodshed's Dev-C++ IDE and Compiler is more user-friendly, and easier to navigate.
But don't take my word for it, try it out for yourself:


(Windows Compatible)
Microsoft Visual C++:
Overview: Download details: Visual C++ 2005 Redistributable Package (x86)
Download: http://www.microsoft.com/downloads/...-3266-4157-a2ec-5174867be706/vcredist_x86.exe

(Windows Compatible) (Reccomended)
Bloodshed's Dev-C++:
Overview: Bloodshed Software - Dev-C++
Download: SourceForge.net: Downloading ...

Okay, so I'm going to be writing a tutorial as if you were using Dev-C++ v5.0, although if you have any other version of Dev-C++ or even a different compiler, the simple things like Compile and Run are pretty much simple.

So, lesson #1 is going to be teaching you how to compile your first program, and explaining data types.
So I'm going to need you to install the provided downloads given to you, install the programs and then double click the exe to launch the program.
So after you have launched the program, you're going to want to create a console application (the next few tutorials we will be using console application).
So go to File > New > Project... and then select Console Application, and name it 'MyProject' or anything you want (It's just a name), but it's best to keep a directory, for easy access and organisation. Once you've given a name to your project, make sure that 'C++ Project' is selected. Once you have done all of this, click 'Ok'.

Now, you should have a window with the following code in it:
Code:
[B][COLOR="Red"]#include <cstdlib>
#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
    system("PAUSE");
    return EXIT_SUCCESS;
}
[/COLOR][/B]
...or some similar code.

What I want you to do is highlight it all and delete, so your project is left empty.

Okay, so after that's done, I want you to take a look at this code:
Code:
[COLOR="Red"][B]#include <iostream>
using namespace std;[/B]
[b]int main()
{
cout << "Hello There RaGEZONE\n"; [/B][/COLOR] [COLOR="SeaGreen"]// Outputs the sentence "Hello There RaGEZONE")[/COLOR]
[B][color="red"]cin.get();[/color]   [color="seagreen"]//  Requests user to hit Enter key to end application[/color]
[color="red"]}[/color][/b]

Anything familiar about the above code? If not, no worries at all :) This is the reason I'm here, to teach you what this means and how to use it :)

That's start with #include <iostream>.
#include <iostream> is something called a preprocessor directive that requests code from that header file to put into our application.
By including a header file, it provides you with the use of certain functions, such as cout.


The next line of code is: using namespace std;
using namespace std; is similar to that of <iostream>. Basically, this line of code is something called a standard library. Using this line, it allows you to use certain functions such as cout. (See the similarities?). Also, notice the semi-colon ( ; ) on the end of the the line of code. This is apart of C++'s syntax and is used for most commands.



Now the next line is int main ().
This is the beginning of the main functions. It's where the C++ program's start their execution (depending on the location of the code).
int is for when the fuction returns an integer.
The braces ( { } ) bascially defines the beginning and the end of the execution.
Everything after { is what will be executed, everything before } is what has been executed.

Note: When I say "Executed" it basically means performed/done/run.


cout << "Hello There RaGEZONE\n";
Now what this is doing is outputting the phrase "Hello There RaGEZONE"
Think of the statement cout as output.
cout is declared in the iostream standard file within the std namespace, this is the reason why we needed to include that specific file and to declare that we were going to use this specific namespace earlier in our code. (Every line of code is done for a reason). The bracket things << are insertion indicators, indicating what our output is going to be.
The \n means newline. What it does is seperate output on the screen, so it doesn't look disorganised:
Example:
Code:
[COLOR="Red"]cout << "Hello Ragezone";
cout << "I'm Awesome";[/COLOR]
This would output as:


Now, if you wanted a space between Ragezone and I'm, you would have to make sure you left a space in one of them:
Code:
[COLOR="Red"]cout << "Hello Ragezone ";
cout << "I'm Awesome";[/COLOR]
or
Code:
[COLOR="Red"]cout << "Hello Ragezone";
cout << " I'm Awesome";[/COLOR]
Or if you wanted it to be one line of code:
Code:
[COLOR="Red"]cout << "Hello Ragezone\nI'm awesome";[/COLOR]
Output:

Note: Notice how the \n (newline) is ALWAYS within the double quotation marks? This is a Must!

Note: C++ is a case-sensitive language, so everything has to be the way it's supposed to be:
Cout = wrong
cout = right

//
This is simply to mark line notes. They don't affect the application in anyway, they're only used for viewers of the code.
/* this is
a
blocknote*/

A blocknote is a... well, a note formatted as a block. This is used for full, descriptive explenations of a piece of code.

cin.get();
(Notice the ; ? It's been used throughout this application, remember what I said? It's used on functions).
Without placing the cin.get(); , the application window will instantaneously open and close. By placing cin.get(); at the end (before the end bracket), it means the application needs the user to press enter for the application to close.

After you have typed all of this code in, and know what everything is, try mesing about with the code a bit. Insert \n with new phrases. Learning C++ is all about exploring. I'm going to give you a guideline but you will have to explore solo a bit if you want to fully understand C++'s nature :)

And for the end part:
After you are happy with your codes, go to Execute > Compile & Run name it to whatever you want and there you have it! Your first C++ application.
This is the end of C++ Tutorial #1
I will be writing C++ Tutorial someime during the 20th of August.

What will be in Tutorial #2?:

You will be learning about data types, becoming more familiar with console applications and functions within. I will teach you how to use declare and define, how to manipulate integers and will result in teaching you on how to make a calculator

C&C Would be greatly appreciateed :)

Any questions about this tutorial or future tutorials are welcomed and will be answered ASAP :)

Credits: Me...



Instead of cin.get(); you could also use System("PAUSE"); as that waits for a user to touch a key.
 
Re: C++ Tutorials: #1

Instead of cin.get(); you could also use System("PAUSE"); as that waits for a user to touch a key.

You could also use the return function :)

And, tbh I would say C++. Although C# is gaining popularity...
 
Nice tutorial, should get ppl starting to use this programming language :).

I use Code::Blocks IDE - Open Source, Cross-platform Free C++ IDE, which is quite similair to Dev-C, but I prefer CB :). Perhaps you can add that to the list of possible IDE's :).

C#: have heard positive and negative reactions about it...I prefer C(++) :P


The system("PAUSE"); is windows only, right? I used .bat files for this before, but this sounds like a better solution ;), although than its not cross-platform anymore...
 
Re: C++ Tutorials: #1

Ahh no problem bud :) Glad I inspired someone.

And yeah, any questions/queries, don't hesitate to ask.

I'm not sure what I'm going to do for Lesson #4 (I already know what #3 is).
So, any suggestions? What would you like to know about C++? Anything you guys would like to make?
Yea it would be nice learn how to make programs in C++ with a GUI not using the cmd :) But I wont say much more, I'm looking forward to the next lesson, professor =]
 
Re: C++ Tutorials: #1

Yea it would be nice learn how to make programs in C++ with a GUI not using the cmd :) But I wont say much more, I'm looking forward to the next lesson, professor =]

With C(++) you can't make a GUI (hardly can), cos its processor only. To use a videocard you can use WIN32 (for Windows only, requires loads of code ^^) or OpenGL/SDL. OpenGL has extensions like GLUT (outdated) and few others to manage windows. Works fine. SDL is good too (I use that now), it can handle windows, sounds, events (mouse, keyboard etc) etc, which is very nice. Than to display an 2D/3D environment I init OpenGL within SDL. But you can also use SDL for 2D/3D stuff.

Well, thats how I use it...and you KyleEnos? ;)
 
Re: C++ Tutorials: #1

With C(++) you can't make a GUI (hardly can), cos its processor only. To use a videocard you can use WIN32 (for Windows only, requires loads of code ^^) or OpenGL/SDL. OpenGL has extensions like GLUT (outdated) and few others to manage windows. Works fine. SDL is good too (I use that now), it can handle windows, sounds, events (mouse, keyboard etc) etc, which is very nice. Than to display an 2D/3D environment I init OpenGL within SDL. But you can also use SDL for 2D/3D stuff.

Well, thats how I use it...and you KyleEnos? ;)

Easy:...
DirectX :]

EDIT: Late on, I will be teaching you how to apply an interface to your applications. It isn't as hard as everyone makes it out to be. It's like math:
Atm you only know 2+2, and when people are telling you 12x12 is hard, you genuinely believe so, until you begin to unerstand.
Once you know the basics and the foundation, you can do anything :)
 
DirectX, are you focused on Windows? :P I always want it to be cross-platform, thats why I use SDL. And yes, once you know the basics its very easy to add menu's, icons, statusbar and all that stuff ;). Looking forward to ure next tut :D
 
I'm writing #2 as we speak.
And yes, games, expecially MMOs are known to have been built with C++. Such games as: WoW, GW EVE and Anarchy.

And as I said, I can't jump from simple text output to advance GUI apps :P I will come to it though, don't worry =]

And Daeivius, yeah I do mostly use DirectX, but am known to sometimes use OpenGL :)
 
Last edited:
Back