# Total Begginer C++ Tutorial

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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;

}


REMEMBER! A functional C++ programmer always indents his / her code. After the brackets, hit the space bar twice to indent your code. After you do it once, the computer will do it automatically for you. Before the initial "cout" text, you use <<, and after the "cout" text you use <<. The "endl;" stands for End Line, that tells the computer that there will be no more text on that line. You ALWAYS end commands in your code with a semicolon. The "return 0;" is telling the computer to return "main()" to 0. Basically after the initial program is finished the computer has to clean up its mess. If you compile this code, you may have noticed that the screen flashes for a split second then disappears, that’s because we have not include a "pause" command. There are a few ways to pause the screen on a console app. You can use the "getchar();" command, or the "system("PAUSE”);" command. I would recommend the "getchar();" command, because it just pauses the screen until the users presses a key, but the "system("PAUSE");" command displays "press any key to continue..." and that gets annoying. You can also add comments to your code by using the "//" followed by your comment. The compiler ignores all comments, because they are only for the benefit of the programmer and others. So if you actually wanted to read the text, instead of it flashing for a split second, here is how you would go about doing it:
// 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;

}


Congratulations! You have successfully learned how to create your first C++ program. You now know how to set up a basic program structure, add comments, display text on the console screen, and pause the console screen. Now we are ready for input… Basically, input is just the computer asking you to enter in something. Before you even ask for input, you must tell the computer that you are going to ask for input. So after the first brace, you must use a string followed by the name of the string that you are going to use. In your code, to ask for input, you would use the "cin" command. "cin" stands for C++ Input. You always follow "cin" with >>, for example:
// 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
[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
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
{ } 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
Thanks for the advice everyone, I have fixed most of my mistakes. I'm glad that people like my tutorial. ;)

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

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

##### Share on other sites
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
If you don't have a #include<string> vc6 will give a compiler error.

##### Share on other sites
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
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
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
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
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
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
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

##### Share on other sites

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.