$pagetitle="HOW TO USE VISUAL STUDIO 2017 TO CREATE A SIMPLE CONSOLE APPLICATION"; $pageid=98; $sitelink="http://www.learn-coding.today/visual_studio_2017.php"; $page="visual_studio_2017.php"; include('includes/headerzero.php'); ?>
By Sergey Skudaev
Download Visual Studio Community 2017: https://www.visualstudio.com/downloads
It may take two hours depending on your PC speed and memory. Let´s start Visual Studio and create a simple C++ console application that will display a text in the command prompt window. Before, I would create an empty folder and name it c_plus_plus. In this folder you will create all your C++ projects.
When Visual Studio opens, select File -> New -> Project.
Figure 1. Creating a new project.
A new project window is displayed. Select Visual C++ on the left panel and Window Console Application on the central panel.
Figure 2. Selecting a Windows console application.
Enter project name, mark "Create directory for solution" check box and select the c_plus_plus folder. Click the OK button. A new project will be created in the tree folder.
Figure 3. A simple C++ console application.
The Solution explorer on the left includes the project title, references, External Dependencies, Header Files, Resource Files, and Source Files. Now you have to edit the Tree.cpp file. For now, ignore all folders and files except the Tree.cpp file and the StdAfx.h file. Copy the following code into the Tree.cpp file
#include "stdafx.h"
#include <iostream>
using namespace std;
int main(int argc, char* argv[])
{
printf(" @@@\n");
printf(" @@@@@\n");
printf("@@@@@@@\n");
printf(" @@@@@\n");
printf(" |\n");
printf(" |\n");
printf("UUUUUUU\n");
int hold = 1;
std::cin >> hold;
return 0;
}
The ´\n´ characters are creating a new line of text.
Figure 4. The Tree.cpp file with a new code.
Now the Tree.cpp file has a new code. Click the blue diskette icon on the main menu to save changes. To run the code, click Debug->Start Debugging or press the F5 key.
Figure 5. Run the Tree.cpp code.
Figure 6. Would you like to build it?
The "Would you like to build it?", message is displayed. Select the Yes button.
Figure 7. The program output is displayed in the command prompt window.
You can edit command prompt window properties. Click the right mouse button on the title bar and select properties from the drop-down menu.
Figure 8. Changing the command prompt properties.
The properties window is displayed. You can change font type, size and color.
Figure 9. The properties window.
Figure 10. The blue command prompt window with the white characters.
You can see that the Tree.cpp file has two lines with #include directive. It means that stdafx.h file and iostream library are included in our code
#include "stdafx.h"
#include <iostream>
If you open the stdafx.h file you will see that it has included directives as well.
#include "targetver.h"
#include <stdio.h>
#include <tchar.h>
The stdio.h library is required for the printf function. The iostream library is required for the cin function (pronounced as C IN) and cout functions (pronounced as C OUT).
You can print the tree in your code using the cout function instead of the printf function.
Add the following code in bold to the Tree.cpp file:
int main(int argc, char* argv[])
{
printf(" @@@\n");
printf(" @@@@@\n");
printf("@@@@@@@\n");
printf(" @@@@@\n");
printf(" |\n");
printf(" |\n");
printf("UUUUUUU\n");
cout <<" @@@"<< endl;
cout <<" @@@@@"<< endl;
cout << "@@@@@@@" << endl;
cout << " @@@@@" << endl;
cout <<" |" << endl;
cout << " |" << endl;
cout << " XXXXXXX"<< endl;
int hold = 1;
cin >> hold;
return 0;
}
The cout function is used to output text on the screen. The cin function is used for input. In our code we need to include the cin function because it stops running the program and waits for a user input. If we did not include that line, then out program would display the tree for a quick moment and the command prompt window would close. Comment two lines:
//Int hold=1;
//cin>> hold;
And run the code again. You will see the command prompt window with the tree only for a moment and then it will close.
The line "using namespace std;" is required because the cin and the cout functions belong to a namespace called "std". You may not use (comment out) the line "using namespace std;" if you call the cin and the cout functions as std::cin and std::cout. See Figure 11.
Figure 11. Using the printf and the cout functions to display text on the command prompt window.
The red text after 2 forward slashes is a comment. In most programming languages comments are posted after 2 forward slashes like that:
// this is a comment
Or a multi line comment can be entered like this:
/* this is a comment
this is a comment */
If you comment out the include directive with two forward slashes and rebuild the project, a fatal error will be displayed. Then, if you uncomment the directive and rebuild it all again, you will get no errors. To close the project, select File-> Close Solution.