# Total Begginer C++ Tutorial

## Recommended Posts

NoWhereMan    229
To all C++ beginners / total C++ nOOB’s, I have been reading many posts asking for a simple C++ beginners tutorial, so I thought that I would wright my own tutorial. Ok, welcome to the C++ beginners / total C++ nOOB tutorial. Congratulations, you have chosen the most widely used programming language in the world at the moment. C++ is not only an industry standard for game developers, but for software developers as well. This short tutorial is designed to teach you the basics of C++. Shall we begin? So basically, computers are whiny crybabies that need to be told exactly what to do in the exact order that you want them to do it. That’s right, if you don’t tell them, it won’t happen. First of all, you need to tell the computer what tool’s it will need, like telling someone what materials they will need for a certain project. That is where we get "#include" commands. Your telling the computer what it will need to "include" to carry out the other instructions. Some basic examples of these "#include" are: #include <string> - This lets the programmer use strings in his / her main code #include <cstdio> - This is telling the computer to include the "C Standard Input / Output" #include <cstdlib> - This is telling the computer to include the "C Standard Library" #include <iostream> - This is telling the computer to include the "Input / Output Stream" (You can tell that programmers LOVE acronyms) Another common command that you must tell the computer is the "using namespace std;" command. After telling the computer what you need to include, you must set up the basic structure of the program. In simple programs you start with "int main()." After the "int main," you open the program and close the program with braces:
#include <iostream>
using namespace std;

int main()

{

}


In between the brackets, you would include all of your code. To instruct the computer to display text on the screen, you would use "cout." "cout" stands for C++ Output. Right now, the text is going to appear on the console screen. The console screen is basically a DOS window. Here is an example of displaying text on a console screen:
#include <iostream>
using namespace std;

int main()

{

cout << "This text will appear on the console screen!" << endl;

return 0;

}


// This is an example of a comment, you should add comments to your code as much as
// possible so that you can figure out what you were thinking of later

#include <iostream>
#include <cstdlib> // in order to use getchar(); you must include the C++ Standard Library
using namespace std;

int main()

{

cout << "This text will appear on the console screen, and you can read it!" << endl;
getchar();

return 0;

}


// A program that asks for your name using C Input

#include <iostream>
#include <string>
using namespace std;

int main()

{

string name;

cout << "What is your name?" << endl;
cin >> name;

return 0;

}


This program only displays, "What is your name?" Then it asks you for your name and then closes. If you would like to display an inputted variable, you would use "cout <<" followed by the string name and ending with "<< endl;" You can clear the console screen by using the "system("cls");" command. The following example does just that:
// A program that asks for your name using input,
// then displays your name using output
#include <iostream>
#include <string>
#include <cstdlib>
using namespace std;

int main()

{

string name;

cout << "What is your name?" << endl;
cin >> name;

system("cls");

cout << "Nice to meet you ";
cout << name << endl;
system("PAUSE");

return 0;

}


As you can see, if you do not end the line using "endl;" then the next "cout" stays on the same line. In this case, getchar(); will not pause the screen, so we must use system("PAUSE"); You have now learned the fundamental basics of C++. I encourage you to keep learning and to keep programming! [Edited by - NoWhereMan on September 30, 2004 8:51:29 PM]

##### Share on other sites
mikeman    2942
[cool] OMG! Thx dood! I am now a l33t haxoR!!11 [cool]

Seriously, I thought it was pretty good. Maybe you should have spend a little more time explaining the notion of variables, I don't know how fast a noob can understand that.

##### Share on other sites
NoWhereMan    229
Quote:
 Original post by mikemanSeriously, I thought it was pretty good. Maybe you should have spend a little more time explaining the notion of variables, I don't know how fast a noob can understand that.

Ok thanks alot, I'll take your advice and try to fix that.

##### Share on other sites
Well, I'm just trying to teach c++ to a friend of mine.
Might refer him to this thread as well ;)

btw. : cstdio = c standard IO not c studio =)

##### Share on other sites
GroZZleR    820
{ } are called braces, curly braces or curly brackets.
[ ] are called brackets.
( ) are called parenthesis.

Might want to clean that part up, the rest of it looks good though. Good job!

##### Share on other sites
NoWhereMan    229
Thanks for the advice everyone, I have fixed most of my mistakes. I'm glad that people like my tutorial. ;)

##### Share on other sites
NoWhereMan    229
Should I make another tutorial, a more advanced one?

##### Share on other sites
Eriond    187
To use string, include string :) You didn't do that in your example.

##### Share on other sites
NoWhereMan    229
Quote:
 Original post by EriondTo use string, include string :) You didn't do that in your example.

EDIT: Sry my mistake, there will be a compiler error if you are using VS6.

[Edited by - NoWhereMan on September 30, 2004 5:37:22 PM]

##### Share on other sites
twanvl    512
If you don't have a #include<string> vc6 will give a compiler error.

##### Share on other sites
Seriema    634
the std libraries can include other std libraries. But that's not defined. So you shouldn't count on it. Always include the header you're going to use. That may work on your compiler (where their implementation includes 'string' in 'iostream'), but maybe not on your new learner which will be kinda confused.

##### Share on other sites
Washu    7829
Quote:
Original post by NoWhereMan
Quote:
 Original post by EriondTo use string, include string :) You didn't do that in your example.

Are you taking about #include <string>?

That's not necessary here, all you need to do is:
*** Source Snippet Removed ***

It works just fine, and this tutorial is intended for complete beginers.

• It is necessary.

• If you are going to write a tutorial for complete beginners, make sure it is completely correct so that they don't get completely weird errors with their completely incorrect code.

But hey! Atleast you're trying. Just make sure your information is correct before publishing it, and all will be well.

##### Share on other sites
Fruny    1658
Quote:
 Original post by NoWhereManAre you taking about #include ?

Yes

Quote:
 That's not necessary here, all you need to do is:

Nothing in the C++ standard says that <iostream> has to define std::string.

Quote:
 It works just fine

Not on my machine.

Quote:
 and this tutorial is intended for complete beginers.

Which makes correctness even more important, misleading a beginner is the worst disservice you can give them.

##### Share on other sites
elementary    154
Quote:
Original post by Washu
Quote:
Original post by NoWhereMan
Quote:
 Original post by EriondTo use string, include string :) You didn't do that in your example.

Are you taking about #include <string>?

That's not necessary here, all you need to do is:
*** Source Snippet Removed ***

It works just fine, and this tutorial is intended for complete beginers.

• It is necessary.

• If you are going to write a tutorial for complete beginners, make sure it is completely correct so that they don't get completely weird errors with their completely incorrect code.

But hey! Atleast you're trying. Just make sure your information is correct before publishing it, and all will be well.

The guy obviously hasn't got a clue what he's talking about, and shouldn't be writing tutorials.

##### Share on other sites
Washu    7829
Quote:
Original post by elementary
Quote:
Original post by Washu
Quote:
Original post by NoWhereMan
Quote:
 Original post by EriondTo use string, include string :) You didn't do that in your example.

Are you taking about #include <string>?

That's not necessary here, all you need to do is:
*** Source Snippet Removed ***

It works just fine, and this tutorial is intended for complete beginers.

• It is necessary.

• If you are going to write a tutorial for complete beginners, make sure it is completely correct so that they don't get completely weird errors with their completely incorrect code.

But hey! Atleast you're trying. Just make sure your information is correct before publishing it, and all will be well.

The guy obviously hasn't got a clue what he's talking about, and shouldn't be writting tutorials.

While this may be true, you are none the less trolling a thread. Which is an even worse offense on these boards. Please try and keep it to a minimum.

##### Share on other sites
NoWhereMan    229
Sorry, my bad, I have only been working with the Dev-C++ IDE, and have not used VS6.

I will try to be more precise before releasing a tutorial of any kind.

##### Share on other sites
demonkoryu    980
Quote:
 Original post by GroZZleR{ } are called braces, curly braces or curly brackets.[ ] are called brackets.( ) are called parenthesis.

Hey thanks, finally explained!

Nowhereman: Keep it going! I think its a pretty good intro.
Quote:
 Another common command that you must tell the computer is the "using namespace std;" command.

Needs more explanation.

Thermo