Beginner's Guide to Programming - guidetoprogramming.com

  • Increase font size
  • Default font size
  • Decrease font size

C++ Tutorial 1 - Structure and some common commands

In this tutorial, we are going to write a simple program, and then break down what each line is doing.  We will also go over some of the common commands you will see.

When writing programs, it is always a good idea to include comments to explain what you are doing.  When you are looking back 6 months from now, it will make it much easier to realize what you were trying to accomplish with each section of code.  It will also help anyone else reading your code understand if you know what you are trying to accomplish.  It can be very difficult to understand code without comments, even if you were the one who wrote the code!  You will quickly learn this as you write programs - there are times when I am looking at my code 15 minutes later (after I have moved on to another section or taken a break) and have to take a minute or two to figure out what I was doing! 

For the example below, I am going to include way too many comments in order to explain line by line.  Don't over think your quantity of comments initially - it will become apparent to you over time how best to comment your code.  Reading the code of others will help as well.

Let's go on to the tutorial code!

The uncommented program we will use is below:

#include <stdio.h>

int main()
{
  printf( "This is some text.\n" );
  printf( "Press Enter to continue.\n" );
  getchar();
  printf( "Press Enter to exit program.\n" );
  getchar();
  return 0;
}

 

Copy the above code and paste it into your text writing program.  Notepad will work for this - I use Textpad, which is available as a free trail download.  Save the file as "demo1.cpp".  When you run it through your complier in Windows it will produce "demo1.exe".  When running code through the G++ compiler in Linux, you specify the output file name using the convention "g++ <sourcefile> -o <outputfile>".  In both Windows and Linux, the file will automatically be executable.  Run the program and you will get this output:

 

This is some text.

Press Enter to continue.

Press Enter to exit the program.

 

 Below you will find the same code, but with extensive comments explaining what each part of the code does:

//include statements tell the compiler what libraries of functions to reference in your programs
//in this case, "stdio.h" calls the standard in/out fuctions, which read in data, print out data, etc

#include <stdio.h>

// putting the double slashes in front of your code inserts a comment in C++
/* For comment in C, you need a slash and asterisk before and an asterisk and slash after.
This also works in C++ */

/* When using this style, the comment stays active until you close it, so be sure not to
include your code in here!

For example, this is still the comment.

So is this.  The comment ends here */


//I find the double slashes easier to use, as you can identify every line that is a comment

//the main function is the beginning of the program
//"int" specifys what the programming is returning - in this case nothing, so take it on faith for now!

int main()
{
  //printf prints text to the currently selected output destination, which is the screen by default
  printf( "This is some text.\n" );
  printf( "Press Enter to continue.\n" );
  //getchar will grab some text from the screen, ending when the user presses <Enter>
  //in this case, we aren't using the input for anything, we are just using getchar() to pause the program

  getchar();
  printf( "Press enter to exit program.\n" );
  getchar();

  //the return below is the "int" that was referred to above.  In this case, the int (integer)
  //isn't being used for anything.  In many cases, a function will analyze data and return a result,
  //in which case the return is used to pass the result back once the function is complete.

  return 0;

}

 

You can actually copy the above, paste it into your text editor, and compile it as code.  It will work, as it is the same exact instructions with comments included!  The comments explain what the commands of the program are doing.  Take some time to read through it, and feel free to experiment with your own edits - try adding additional lines or additional pauses.  

 

Congratulations - you have completed the first tutorial!