Tuesday, July 28, 2015

How do you create a C++ Windows Console Application in Visual Studio 2015?

In a recent post I described why you cannot use Visual Studio Express 2015 Community for developing C++ Windows Console Application.

You need Visual Studio Express 2015 for Windows Desktop to develop the C++ Windows Cosole application. Downloading and installing the same and the various templates that you can use are described here .

Launch Visual Studio Express 2015 for Windows Desktop (just VSDesktop in this post) from its shortcut.Click on New Project... to open the New Project window and expand the Visual C++ node as shown highlighting the Win32 Console Application. Change the name of the project to Console2015 and accept default directory for saving.

Console2015_01

Click on Win32 Console Application to display the Welcome wizard screen as shown.

Console2015_02

Click Next button at the bottom of screen to display the Win32 Applicaiton Wizard screen as shown.

Console2015_03

Accept all items on the above screen and click Finish.

 
The project window opens as shown with the Console255.cpp file on the left and the project folders/files in the Solution Explorer as shown. This should build without errors.
Console2015_04

Double click and view the stdafx.h file. Add the indicated (in red) '#include' files as shown as we are planning to use the console as well as 'string' variables.
----------------------
#include "targetver.h"
#include
#include
#include
#include

// TODO: reference additional headers your program
//requires here
----------------------
Open the Console2015.cpp by clicking the same in soltuion explorer.

Add the followind code to this file as shown. 'endl' adds an empty line to the output.
--------------------
#include "stdafx.h"

int main()
{
 std::string mystring = "Hello, from Waikiki";
 std::string mystring2 = "This is Jay";

 std::cout << mystring2;
 std::cout << std::endl;

 std::cout << mystring;
 std::cout << std::endl;
 return 0;


}

--------------------
From Main menu click Build | Build Solution. You should see the
following in the Build Outpt screen.
--------------------
1>------ Build started: Project: Console2015, Configuration: Debug
Win32 ------
1>  stdafx.cpp
1>  Console2015.cpp
1>  Console2015.vcxproj -> c:\users\jayaram\documents\visual studio

2015\Projects\Console2015\Debug\Console2015.exe
========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped

==========
-------------------------
Hit Start without debugging (CTRL+F5) to run the program. The output
is displayed in the console as shown.

Console2015_06

By using the namespace designation the above code can be modified as
shown (stdafx.h stays uncchanged):
----------
// Console2015.cpp : Defines the
// entry point
// for the console application.
//

#include "stdafx.h"
using namespace std;

int main()
{
string mystring = "Hello, from Waikiki";
 string mystring2 = "This is Jay";

 cout << mystring2;
 cout << std::endl;

 cout << mystring;
 cout << std::endl;
 return 0;


}

--------------------------------------


 

No comments: