Hello world in C++ , by Jeremy Red

Posted on 2017-08-07 19:01:26 UTC

Tags: C++ Beginner Tutorial Hello_World First_tutorial

Introduction to C++ in visual studio 2017

Hello there, this tutorial is intended for beginners to programming, C++ or visual studio.

Prerequisites:

-Visual Studio 2017, C++ must be included(Community edition is free and can be downloaded here ).

-A computer that can run visual studio 2017

-A learning attitude.

1. Launch visual studio

Double click on the visual studio icon on the desktop or launch it by searching in the start menu

2. Set up the environment

In the upper right corner of visual studio click on file->new->project (As in the example below)

Next is to create the project under installed->visual C++->Windows Desktop->Windows console application.

Press the ok button now to create your project or hit the enter button on your keyboard.

Start your first line of code in C++

Now that your project has been created it should look something like this

Let me explain a bit about this here project. The .cpp file that was created is the source of your program, in other words its a script you can use to tell the computer what to do(Just remember it always does exactly what you tell it to). The lines that are greened out are called comments(with the // in front). Comments are ignored by the compiler completely, so remember in the future when your are following programming tutorials anything that is a comment does not have to be copied at all(You can be lazy with it since you are learning anyways). The line ' int main()' is declaring a function that always needs to return an int(Think of it as a whole number for now), the number that needs to be returned is called by the line " return 0"(In C 0 usually means the program exited with no failure). The brackets { and } are the beginning and the end of a function in C and C++. A main needs to be declared in every program you write(Unless its a library meant to be called in another program but that's not important for now).

In order to print to the console in C++ you must first use the pre pre processor #include, in C++ a pre processor is something that is interpreted by the compiler before the code is actually compiled into a executable format. In order to print to the console we will be using the standard library which is used for displaying and getting input from the user. so write the following code at the top of your source.cpp file.

#include <stream>

Now inside your int main function write the folliwing code and then run it.

std::cout << "Hello world!";

What happend? The window instantly closed, that is becuase the program does exactly what you tell it to. It does not do what you want it to. To fix this we will add a delay at the end by typing the following code

int i=0;
while(i++ < 100000);

Now run it, the program will now display hello world breifly before exiting. The is however a few problems with this. Problem number one is that this code is not portable, the amount of time it takes for the loop to execute will vary on different machines. The other problem is that some machines might run it so fast you still can not see it print it. We will fix this by instead using cin.get() to wait for the user to press a key and then before exiting. replace thw while loop with the following line of code

std::cin.get();

Now it should run fine and not exit untill a user presses a key.

Congratulations you now understand the basics of being a C++ programmer, leave a comment below with feedback or ideas on how i could improve this tutorial, Thanks for follwing this guide!.

  • 2017-08-13 21:17:17 UTC | by Jeremy Red
    This is a great tutorial thanks
  • 2017-09-17 18:56:57 UTC | by Jeremy Red
    Please note i will be improving this tutorial in the future.
  • 2017-10-05 15:24:52 UTC | by Jeremy Red
    I fixed stream not showing up after include, apparently writing triggered a non existent html tag which is then parsed out by the browser. Sory if anyone tried to complete this tutorial and was unable to because of this mistake.