Anda di halaman 1dari 6

Laboratory 0 Week 0

Structured Programming
An Introduction to Visual Studio and C++
0.1 Introduction

This is a short session to familiarize working with the Visual studio programming and development
environment. You can use Visual Studio to create many kinds of applications, from simple store apps
and games for mobile clients, to large, complex systems that power enterprises and data centers.
You can create:
1. Apps and games that run not only on Windows, but also Android and iOS.
2. Websites and web services based on ASP.NET, JQuery, and AngularJS.
3. Applications for platforms and devices as diverse as Azure, Office, Hololens, and Kinect.
4. Games and graphics-intensive applications for a variety of Windows devices, including Xbox.
Visual Studio by default provides the support for C#, C and C++, JavaScript, F#, and Visual Basic. Here
we are going to work with C++.

0.2 Preliminaries
Make sure you have access to your home directory. Visual Studio should create a place for all of your
workspaces in c:\documents\visual studio\Projects. Check that in this case.

0.3 Starting and Ending Visual Studio

You can find out which edition of Visual Studio is right for you at Visual Studio Editions.
You can install Visual Studio 2015 by downloading it from Visual Studio Downloads. If you need to know more
about the installation process, see Installing Visual Studio 2015.
Note that any version you download from the Visual Studio website might be slightly different to the versions
that we are running on the department machines, but all of the concepts should be the same.
On the department machines, Visual Studio can be found through the following menus:
Start Button-All Apps Visual Studio
This program works similar to any other software product, i.e. you open up the program, and you then either
create a new program or load an existing program.
When you have finished, you exit Visual Studio by selecting File-Exit.

Structured Programming Page 1 of 6


0.4 Visual Studio IDE

When you create an application in Visual Studio, you first create a project.

To create a console application deploy the following steps

1. On the menu bar, choose File, New, and Project.

2. In the Visual C++ category, choose the Win32 Console Application template, then name the project
and Press Ok.

3. When the Win32 Application Wizard appears, choose the Finish button.

Structured Programming Page 2 of 6


The project with the basic files for a Win32 console application is created as shown below.

The components of the Visual Studio IDE that we will be working with are as follows:
a) Code Editor Window for editing source code manually.
b) Error List Window for displaying information about a specific error message.

Next, you'll add code in the Code Editor Window.

Structured Programming Page 3 of 6


To display Welcome to Lab 0 in the console window

1. In the code editor window enter a blank line before the line return 0.
2. Enter the following code cout << "Welcome to Lab 0\n";

A red squiggly line appears under cout. An error message appears if you point to it.

This error message cout is included in the <iostream> header file also appears in the Error List
window. You can display the window by going to the menu bar, choosing View, then Error List.
#include <iostream>
3. To include the iostream header, enter the following code after this header
using namespace std;
#include "stdafx.h".

You probably noticed that a box appeared as you entered code, providing suggestions for the characters
that you entered. This box is part of C++ IntelliSense, which provides coding prompts, including listing
class or interface members and parameter information. You can also use code snippets, which are pre-
defined blocks of code. For more information, see Using IntelliSense and Code Snippets.

Note that: The red squiggly line under cout disappears when you fix the error.

4. Save the changes to the file.

You can debug your application now to see whether the sentence Welcome to Lab 0 appears in console window.

Structured Programming Page 4 of 6


To debug the application

Start the debugger by choosing Debug from menu bar, and Start without Debugging

The debugger starts, and a console window appears showing as below.

Note: You can press SHIFT + F5 to stop debugging.

Structured Programming Page 5 of 6


The components of this simple program are as follows:

stdafx.h describes both standard system and project specific include files that are used frequently
but hardly ever change.
iostream provides basic C++ standard library input and output services.
using namespace std is used to replace std:: in each sentence Without using namespace std;
when you write for example cout <<; you'd have to put std::cout <<;.
Program Statements, Every program statement must end with a semi-colon ";".
main is a section where any code within it will be executed when we run our program. The Code needs to be
placed within the braces that follow the definition of main as below:

0.5 Exercises

The following is a short list of tasks aimed at demonstrating some of the features of Visual Studio and C++.

A. Write C++ statements to display on the screen the following patterns

1) ************* 2) ****** 3) @@@@@@@@


********* ***** Good Morning!
***** **** @@@@@@@@
*** ***
* **
*
B. Write a C++ program to print your Name, Level, and Faculty each in different line. Run the program to
see the results. Remove the << after cout Keyword. Try running it again, what happens? Then Remove
the semi-colon ";. Try running it again, what happens? And Finally Remove \n. Try running it again as
well, what happens?

Structured Programming Page 6 of 6

Anda mungkin juga menyukai