You are on page 1of 6

Sultan 

Qaboos University, College of Science 
Department of Computer Science 
COMP2002: Introduction to Computer Science ‐ Spring 2010 
Using Microsoft Visual Studio to write C++ program 
 
1- Under Programs find Microsoft Visual Studio 2008. You should get an introductory screen showing a
Recent Projects section with options to either open or create a project.

2- Click on create and the New Project window will open up. Alternatively, select File Æ New Project …

The following screen will appear:

COMP2002 – Spring 2010     1 
From the left section labeled Project types select Visual C++ and then under it select win32. From the right
section labeled Templates select Console Application. At the bottom section at Location, click on Browse to
specify the location in which the current project folder will be stored. In MS Visual Studio 2008, each C++
program should be created within a project. Each project is stored in its folder. MS Visual Studio 2008 will create
a folder for your newly created project (Notice the tick next to the Create directory folder). At Location, you need
to specify where this project folder will be stored. For smooth execution of your C++ program, store your project
folder under drive D (Data) in a folder you create and named with your ID number. Click on browse and adjust
that folder location information. When done with the location information, enter the name of the project which can
be with the same name of your C++ program. Then click the OK button.

3- The screen that appears next (the above screen) is the Win 32 Applications Wizard. You need to choose
Applications Settings (from left) or click on the Next button.

COMP2002 – Spring 2010     2 
4- You will then see Console Application (checked). You need to check the box for Empty Project. Click
Finish to create your empty project. After you click Finish you will return to a screen from which you can add
a C++ source file into the project.

5- Click Project on the menu bar, and then click on Add New Item.

6- On the screen that appears you need to click on Visual C++ , then code ( from left) and the C++ File (.cpp)
icon (from right). You need also to give a name for your source code file.

COMP2002 – Spring 2010     3 
7‐ When you have completed these steps you will return to the previous screen. You should now see the
directory structure for your project solution (on the right) and a space in which you can type in the C++ source
code. This is the editor window. 
 

 
COMP2002 – Spring 2010     4 
 
8‐ Use the editor window to write your C++ source code. As an example enter the following simple C++ 
program: When finish typing save your program.  
  
//triangle.cpp
//Program computes and prints the area of a triangle
# include <iostream>

using namespace std;

int main () { // main function start here

double base (10), height (15), area;

area= 0.5 * base * height;


cout<<"Triangle area = " <<area<<endl;

return 0;
} // end of program
 
9‐ For a C++ source code to be executed it needs to be compiled first. This can be done by selecting Build Æ 
Compile (or press Ctrl + F7). If the compilation produces any errors (usually called syntax errors) then you 
need to fix them and try to compile again until your program is error free.  
 
10‐ The next step is to build the program. This can be done by selecting Build Æ Build <projectName> where 
<projectName> is the name you specified for your project in step 3 above.  
 
11‐ Finally, the program can be executed by selecting Debug Æ Start without Debugging.  (or press Ctrl + F5) 
12‐ Close current project before creating/opening the next C++ program. Select FileÆ close solution. 
 
Exercise 2: Create a new project called triangle 2 for the following C++ program named triangle2.cpp 
Select File Æ NewÆ project : give project name triangle2. Follow the steps used in creating and executing Exercise 1 
program.  
 
/* triangle2.cpp
Program computes and prints the area of a triangle.
It reads base and height values from the keyboard
*/
# include <iostream>
using namespace std;

int main () { // main function start here

double base, height, area;

//ask the user to enter base and height values


cout<<"Enter base value: ";
cin>>base;
cout<<"Enter height value: ";
cin>>height;

area= 0.5 * base * height;


cout<<"Triangle area = " <<area<<endl;

return 0;
}
 
When you finish executing a program, close its project. Select FileÆ close solution. 
COMP2002 – Spring 2010     5 
Exercise 3: Create a new project hello and enter the following program hello.cpp 
Select File Æ NewÆ project : give project name hello. Then create a new C++ program file hello.cpp 
 
 
/* hello.cpp
Program prints a hello message to the user
*/
# include <iostream>
# include <string> //needed for reading name value

using namespace std;

int main () { // main function start here


string name;
cout << "What is your name?" << endl;
cin>>name;
cout<<"Hello " <<name<<endl;
return 0;
} // end of program
 
Do NOT FORGET to close the project when finished using FileÆ close solution. 
 
Exercise 4: Create a new project FormatOuput and enter the following program FormatOuput cpp: 

// output.cpp
// Explore printing on different lines using endl and \n
#include <iostream>
using namespace std;
int main( )
{
cout << "Welcome to C++\nCOMP2002 lab."
<<endl
<<endl;
cout <<"There are several different"<<endl
<<"ways to format"<<endl
<<"your output"<<endl;
cout <<"\nHow you do it\nis often\nup\nto\nyou"<<endl<<endl;

return 0;
}
 
Exercise 5: Create a new project square and enter the following program square.cpp: 
// square.cpp
//This program computes the area of a square

# include <iostream>
using namespace std;

int main () {
int side; // declare a variable of type integer

// ask the user to enter the input


cout << "Please enter the side length of the square: ";
// get the user input using cin
cin >> side;

// calculate the area and display the result


cout << "The area of the square is " << side*side << endl;

return 0;
}

COMP2002 – Spring 2010     6 

You might also like