Creating Your First C++ Program

Before we begin, you need to get something to code in. For this tutorial, I will be using Visual Studio, but any IDE that can compile C++ will work fine, such as Code::Blocks if you're on Linux or Mac.


After you have your programming environment of your choice installed, create a new (empty) C++ project. How this is done varies depending on what environment you selected, but if you have trouble odds are there are plenty of guides out there.

On Visual Studio for instance, you click "File" -> "New Project" -> "Visual C++" (you may have to install these components if you didn't during the initial installation) -> "Empty Project"


Name this something like HelloWorld, MyFirstProgram, or whatever you think fits.

After that, if a C++ file was not automatically created for you, create one. Again, this varies, but usually it can be achieved by right clicking in the project files and clicking something along the lines of "Add Source" or "Add File". You can name this file anything, as long as the file is a .cpp file.

Now we can finally start programming!


#include

The first line of code we're going to put in is #include <iostream>

What does this do? Well, #include allows us to include files that allow for more functions which is crucial in C++, and many other programming languages. If you have experience with Python, it is much like import.

<iostream> is the name of the file we're including. This happens to be the input/output stream, which allows us to display stuff on the console screen, and have very basic user interaction upon other things.

So now your code should look like this, it's nothing special right now, and it won't do anything if you compile it, but you now know how to include files, which you will be doing in almost every file you create in C++

                    
                        #include <iostream>
                    
                

int main()

In many programming languages, including C++, there is something called a function or method, but I will refer to them as functions. A function is something that when called, will execute all code inside of it.

In C++, the function int main() is called automatically when the program starts, and will execute all code inside of it. Sometimes beginners will start off with a void main() function, but this is a bad idea as some compilers don't recognize that.

So what is int main()? It's anything you want it to be! You can put any valid code inside of it, and that is exactly what we're going to do.

                    
                        #include <iostream>

                        int main()
                        {
                            return 0;
                        }
                    
                

You may notice there is a line that says return 0 right before the function closes off (indicted by the }). This is an exit code for our main function. Depending on the data type the function is (in this case, main is always an integer) it will always return a value (unless it is a void function). Return values are useful for debugging, as you can see what path the program took to get there, but that won't be useful for a while. return 0 means everything went well.

Type (or copy & paste) the code shown above, and then we will move on to actually displaying a message to the user on the screen!


Hello world!

As mentioned earlier, because we included iostream we have access to the input/output stream. This allows us to use objects such as cout (console out), and cin (console in)

Displaying a message is fairly simple in C++, as it is in most languages.

                    
                        #include <iostream>

                        int main()
                        {
                            std::cout << "Hello World!" << std::endl;
                            return 0;
                        }
                    
                

Okay, so it may not look as simple as I made it sound, but I promise it is! Let's start off with looking at std::cout. What's going on here? Well, the first part, std::, is not a disease like you may think! It is a namespace.

A namespace is something you must put before you call an object, function, etc. to specificy what it belongs to. This prevents different files from conflicting with eachother, because if two files had the same name for a function and you called it, the compiler may not know which one to call.

This is the same reason why you may have noticed (if you have looked at other tutorials) that I didn't include using namespace std; at the top of my code. I generally avoid using using, but if you know libraries will not be conflicting you should not have issue.

To make things simple for you, because typing std:: before cout every time can be a pain, you could type using namespace std::cout; below the #include, to only simplify cout instead of the entire std:: namespace.


So what is cout? cout is an object that allows you to output a message on to the screen. cout must be followed by two < (<<) and then your message.

Your message must be enclosed in double quotes, unless it is only one character long. This is so the compiler knows it is a string (text) value. cout has the capability of displaying many types of variables, so the compiler needs to know which one it is.

After your message, you need another two < (<<) and for the sake of simplicity, after that you put std::endl. std::endl ends the line, so the next line displayed will be below the one you just displayed, and it also does something else, which is too technical to get in to right now.

Another option would be to end your string with \n. This allows you to skip the final two < (<<) and the std::endl. \n and endl have some differences, but for a beginner they function virtually the same.

                    
                        std::cout << "Hello World!\n";
                    
                

Another thing about \n is that you can do line breaks in the middle of a string, unlike endl

                    
                        std::cout << "Hello\nWorld\n";
                    
                

The above example would place Hello and World on two seperate lines

NOTE: The console/temrinal does not display \n, it will only display the line break.


If you were to compile this right now (usually clicking something like "Run" or "Debug" at the top of your IDE, on Linux and Mac, you will see Hello World in the terminal when it is executed, and then the generic exit message (returned 0). On Windows however, all you will see is the flash of a command prompt window before it disappears. This is because as soon as it finished displaying the messages, the program returns 0 which terminates int main()

This is a simple fix, which involves another object from iostream


                    
                        #include <iostream>

                        int main()
                        {
                            std::cout << "Hello World!" << std::endl;
                            std::cin.get();
                            return 0;
                        }
                    
                

So, what is std::cin? If you remember from when I mentioned it earlier, cin can be remembered as "console in"

In this case, we're not using cin for anything too special. Currently, it will stop the console from returning 0 until the user presses ENTER (return)


If you have done everything as shown, and your IDE has been set up correctly, you should have a fully functioning C++ program that displays text for a user, and gets input from said user.


Challenge - On Your Own

See if you can create a program that displays your first and last name, and then pauses and asks the user to press enter to continue before it terminates the program.

                    
                        #include <iostream>

                        int main()
                        {
                            std::cout << "FirstName\nLastName\n";
                            std::cout << "Press enter to continue\n";
                            std::cin.get();
                            return 0;
                        }
                    
                

Hover over the box above to reveal code. NOTE: There is more than one correct answer, if it works, it works, but this is one of the most efficient correct answers