How to create new C++ project in Visual Studio 2017

By , last updated May 12, 2018

In this short tutorial we will show how to create a new C++ project in Visual Studio and write and run a simple “Hello, World!” program in C++.

Open Visual Studio

Download and install VS2017 IDE if you don’t have it. Community edition is free to use, but have lesser features. Here’s a comparison of different Visual Studio IDEs.

This is a fresh instance of Visual Studio 2017.

New project

To create a new project, write empty and select Empty Project. Make sure the language is C++.

If it’s not there, then you have to launch Visual Studio Installer to install the C++ features.

New empty project

In the next dialog, write HelloWorld as the project name.

When you are happy with the location and name, press OK.

Add new source file

When the project has been created, add a new item by right clicking Source Files and Add -> New Item....

HelloWorld.cpp

Name the file HelloWorld and make sure C++ File (.cpp) is selected.

Press Add.

Now you are ready to start coding C++ in Visual Studio!

Start by adding these lines of code into HelloWorld.cpp file. This program will print the words “Hello, World!” into the console.

Add the source code.

#include <iostream>

int main()
{
    std::cout << "Hello World!\n";
}

Build the project by pressing F7 and run by pressing F5 or the green triangle in the top bar “Local Windows Debugger”.

Visual Studio console window

Here’s what you should be able to see after you run the program:

If you run the program like that you won’t be able to see the result. It may even appear that Visual Studio “Hello, World!” doesn’t work.

My command prompt keeps opening and closing!

The reason is that the console window in VS appears and disappears really fast. The C++ console window needs to be paused.

In order to see the Visual Studio console result you have multiple choices. Choose one of the following methods to pause the C++ console and keep your Visual Studio console open:

1. Add a break point on the last line of the program.
Click on the grey line to the left from the line numbers. It will add a breakpoint that looks like a red dot:

The program will stop the execution at the line with a breakpoint and you will be able to see the console window. (You must run the program in a Debug mode in order to use breakpoints!).

2. Add system("pause") to your code.
This line will stop the execution of the program until some button is pressed.

#include <iostream>

int main()
{
	std::cout << "Hello World!\n";

	system("pause");
 }

3. Add a std::cin object which is a control input.
The program will be waiting for your input and will finish only after you type something into the console:

#include <iostream>

int main()
{
	std::cout << "Hello World!\n";

	int a;
	std::cin >> a;
 }

Try different “Hello, World!” programs to get started. Follow up with other C++ tutorials.

Professional Software Developer, doing mostly C++. Connect with Kent on Twitter.

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*