Jump to content
  • Advertisement
Sign in to follow this  
NoWhereMan

Total Begginer C++ Tutorial

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!