Total Begginer C++ Tutorial

Started by
15 comments, last by demonkoryu 19 years, 6 months ago
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]
_______________________________________________________________________________________He's a real no where man, sitting in his no where land, making all his no where plans for nobody...
Advertisement
[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.
Quote:Original post by mikeman
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.


Ok thanks alot, I'll take your advice and try to fix that.
_______________________________________________________________________________________He's a real no where man, sitting in his no where land, making all his no where plans for nobody...
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 =)
{ } 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!
Thanks for the advice everyone, I have fixed most of my mistakes. I'm glad that people like my tutorial. ;)
_______________________________________________________________________________________He's a real no where man, sitting in his no where land, making all his no where plans for nobody...
Should I make another tutorial, a more advanced one?
_______________________________________________________________________________________He's a real no where man, sitting in his no where land, making all his no where plans for nobody...
To use string, include string :) You didn't do that in your example.
Quote:Original post by Eriond
To 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]
_______________________________________________________________________________________He's a real no where man, sitting in his no where land, making all his no where plans for nobody...
If you don't have a #include<string> vc6 will give a compiler error.

This topic is closed to new replies.

Advertisement