Introduction: How to Write a Simple C++ Program

In this tutorial, you will learn how to write a program in the C++ programming language. This will go over 10 steps that will explain a simple C++ program. The topics that each step cover in this tutorial are often used in more complex C++ programs. This is only a simple tutorial designed for new C++ programmers and consequently only covers some of the basic topics in C++. C++ programming is one of the most popular languages and consequently, one of the most useful to know.

The following code is the premise for which the tutorial is written on:

// This is a line comment
/*This is
a multiline
comment */

//This has libraries to use input and output
#include

//This has libraries for string operations
#include

//This has standard c libraries
#include

//This includes time libraries for c
#include

int main() {

std::cout << "Do you want to guess a number from 0-9?(yes or no)" << std::endl; // prints to the console
std::string response; // declares a string variable named response
getline(std::cin, response); //gets a line from std::cin and stores it in response

std::srand(std::time(0));  // needed once per program run
int random_number = std::rand() % 10; // gets a random number from 0-9

int guess = -1;
int number_of_guesses = 0;

std::cout << random_number << std::endl;

if (response.compare("yes") == 0) {
  while (guess != random_number) {
   std::cin >> guess;
   number_of_guesses = number_of_guesses + 1;
  }
}
else {
  std::cout << "You're no fun!" << std::endl;
}

if (guess != -1) {
  std::cout << "Number: " << random_number << std::endl;
  std::cout << "Number of Guesses: " << number_of_guesses << std::endl;
  std::cout << "Congratulations you guessed the number!" << std::endl;
}

system("pause");
return 0;
}

Step 1: Download and Install an IDE

The first step to developing your C++ program will be to download an IDE (Integrated Developing Environment). An IDE often includes a compiler, text editor, and often includes a debugger.  Using IDEs makes programming simpler. Three IDEs that I have used and would recommend are the following;

Dev C++
Visual Studio
Eclipse

Here's a link to install DevC++ if you choose to use it:
http://www.youtube.com/watch?v=Y8So6Hh-ZSs

Step 2: Commenting

Though comments in programming don't change how the code works, it is important in communicating what a program does to future developers. Commenting is more important in larger programs, but is also good to use for smaller programs to develop good habits. There are two basic ways to comment. The first is the line comment. Any line that starts with \\ is a comment. Also any code between /* and */ are comments. This is shown in the picture corresponding to this step.

Step 3: #include Directives

After comments, #include statements are written. These lines allow us to specify libraries, or to use code we have written in other files. In the example program, we include a library to use C++ input and output streams, a library to be able to use strings, the c standard library, and a time library. These libraries will enable us to use more operations further on in the program.

Step 4: Main Function

The main function line will be in almost any program you will write. This is where the program will start to run. The main function often is written in the form int main((int argc, char **argv)). This would allow us to pass arguments to our main function, but can be ignored for this program.

Step 5: Variables and Variable Types

In C++, depending on what type of data is being dealt with, different data types might be necessary. The data type used in the picture on this page shows two variables, guess and number_of_guesses, both of type int. They can hold any integer value as their name indicates. There are different other types of variables. Other basic kind of variables include float, double and char. A char can hold a single character, while a float and double can hold decimal values. An example of a char would be the character 'c'. A value that a float or double could store could be the value 1.5. The example program for this tutorial, in addition to using int, uses the type Std::string. This type can hold a sequence of characters.

In the example the value -1 is stored in guess and 0 is stores in number_of_guesses.

Step 6: Printing to Console

In C++, text can be printed to the console by sending data to std::cout. This can include basic data types. The std::endl adds a new line to the output. This is the C++ way to do this. C++ supports most functionality from C. This includes the printf function. Instead of the following code, it could instead be written as the following:

printf("Number: %d \n",random_number);
printf("Number of Guesses: %d \n",number_of_guesses);
printf("Congratulations you guessed the number! \n");

In the printf function, the text entered between the quotes is the text displayed. After the quotes and the comma, the variables printed out are listed. They are printed out in order and must correspond to a %d, %c or other sequence starting with the percent sign. The \n character displays a new line.

Step 7: Reading From the Console

In C++, text can be read from the console by sending data from std::cin and storing in a variable. The console waits until user input when the std::cin function is called. After the user types something in, the program will attempt to store it in guess. In this example, no error checking is done, so if something other than an integer were typed in, the program would likely crash.

Step 8: Arithmetic Operations and Assignment Operator

The assignment operator (= sign) assigns the value from the right side of the equals to the variable on the left side of the equals. For this to work properly, the left side must be a variable.
Arithmetic operations allow mathematical operations to be performed on numbers. There are many operators that can be used to operate on numbers. They include addition (+ sign), subtraction ( - sign), multiplication (x sign) among others. In the line of code, the number_of_guesses gets assigned its previous value plus 1.

Step 9: Conditional (if) Statements

Conditional statements (if statements) change what code runs next depending on what is inside the parenthesis next to an if statement. First the inside of the parenthesis is evaluated. In this instance, if response.compare ( a function from the string library) return 0, the code following it is executed. The compare function returns 0 when the string calling it (response in this instance) is equivalent. Note that strings and basic types use different comparisons. If response is anything other than "yes", "You're no fun!" will be printed to the console. Case does matter.

Step 10: Loops

Loops are almost always used with if statements and run until a certain condition is met. Inside the parenthesis next to the while loop is essentially an if statement. If the statement is true, the program runs until the closing bracket of the while loop and the condition is evaluated again and if it is true, the program runs until the closing bracket of the while loop. This cycle continues until the condition (guess != random_number) isn't true anymore. This loop runs until guess doesn't equal the random_number variable.

Step 11: Final Thoughts

This tutorial left out a lot basics essential to programming in C++, but hopefully was useful in providing an example for some basic C++ programming. If you are more serious about programming, many other websites offer tutorials and can be found through a search through google or another search engine.