Creating C++ Win32 Console Apps

(aka “DOS prompt” or “command prompt” or “command line” programs)

Using

“Microsoft Visual Studio Community 2017”

Integrated Development Environment (IDE)

Section 1 Get the C++ compiler.

1) Install it. There are many versions and flavors of Visual Studio. Visual Studio also contains lots of languages and other tools for many platforms. It can be a bit overwhelming for the beginner. We are looking for “Windows desktop development with C++”.

2) Make sure your pick the correct version: “Visual Studio Community 2017” Start here: https://www.visualstudio.com/downloads/

Click the blue button that says “Free download”. This will download and run the installer program. You may need to look in your downloads folder and run the installer if it does not run automatically. After some minutes you should see a dialog to select “Desktop development with C++” Check the box and click install.

You might also see this “Build an app” thing, click it, it’s a good thing:

3) Eventually after a long time you should see a Launch button. Click It to launch the Visual Studio IDE.

4) You will eventually have to make a free Microsoft Account and sign in but you can also click the “Not now, maybe later” link.

5) Next time, you can start Visual Studio from the Windows Start menu.

Section 2 A Simple C++ Program.

6) When the IDE comes up, if there is a Start Page, close it.

7) From the main menu Click File//New//Project. A New Project dialog window should appear.

8) Select Windows Desktop under Visual C++

9) Select Windows Desktop Wizard

10) At the bottom, under Name, enter some name

11) At the bottom under Location, Click Browse and navigate to some location that you can find later such as your desktop or a memory stick you can bring to school.

12) Click OK. A “Windows Desktop project” dialog window should appear.

13) Under Application type select “Console Application (.exe)” UNCLICK “Precompiled Header”, CLICK EMPTY PROJECT, Click OK

14) Find the Solution Explorer. It has a tree structure with little folders. Right click the little folder labeled “Source Files” then select Add/New Item

15) Select C++ File (.cpp)

16) At the bottom, give it a name ending in .cpp

17) Click Add. A code editor window should appear

18) Type in some code in the editor. The following is correct and should compile:

#include "stdio.h"

void main()

{

printf("Heya!");

}

19) Press F5 to compile (Later CTRL-F5 will keep the output console window displayed)

20) Close Visual Studio. Later, you can open the “Solution File” (.sln) for this project from windows or from Visual Studio.

21) Viola’ c'est fait!


Section 3 Do These Programs

Type in and run the 4 programs shown below:

Turn in:

1) A printout of your source code.

2) A printout of the console output.

Program 1: Write a program to print your name

Using the steps in section 1 above as a guide, modify the source code in the cpp file to print your name instead of “Hello”.

Program 2: Write a program to add two numbers.

Create a program to input two numbers, add them, and print the result. Here is the code:

#include <iostream>

using namespace std;

void main()

{

int num1, num2, sum;

cout << "Please enter first number: ";

cin >> num1;

cout << "Please enter second number: ";

cin >> num2;

sum = num1 + num2;

cout << "The sum is: " << sum;

}

Program 3: Write a program with an “if” construct.

This program will input a number, and tell the user if the number is negative of not. Here is the code:

#include <iostream>

using namespace std;

void main()

{

int num;

cout << "Please enter a number: ";

cin >> num;

if(num < 0)

cout << "The number is negative: ";

else

cout << "The number is not negative: ";

}

Program 4: Write a program with a “loop” construct.

This program will input numbers, and add them to a sum until a -1 is entered. Here is the code:

#include <iostream>

using namespace std;

void main()

{

int num;

int sum = 0;

cout << "Please enter a number (enter -1 to quit): ";

cin >> num;

// this is a loop

while(num != -1){

sum = sum + num;

cout << "Please enter another number (enter -1 to quit): ";

cin >> num;

}

// display the answer

cout << "The sum is: " << sum;

}