0% found this document useful (0 votes)
23 views12 pages

Es036a 07 Lab00

Uploaded by

moh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views12 pages

Es036a 07 Lab00

Uploaded by

moh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Lab 00: Orientation: Introduction to the Visual C++

Environment
ES 036: Programming Fundamentals
Fall 2007
Welcome to ES036 Lab. This guide will help you to get started with using Visual Studio 2005
integrated development environment (IDE) for C++ programming. Please go through the “Lab
Preparation Guide” if you are not familiar with Windows XP and/or WebCT using Internet Explorer.

Table of Contents
A. INTRODUCTION TO VISUAL STUDIO ..............................................................................................1
B. CREATING A NEW PROGRAM FROM SCRATCH .........................................................................10
C. LOGGING OFF AFTER A SESSION ...............................................................................................11
D. GENERAL LAB INSTRUCTIONS TO HELP LABS RUN SMOOTHLY ..................................................11

A. Download Sample Code from Class Website


Use your web browser to navigate to temporary class website (http://instruct.uwo.ca/engin-sc/es036/).
Download MyFirstProgram_v1.cpp If you are not familiar with navigating to class website
using a web browser and downloading files, please go through the “Preparation guide” which will walk
you through those activities.

B. Introduction to Visual Studio


Please follow this section and section C carefully to learn how to use Visual Studio IDE. This section
includes instructions to open, edit, compile, and run the MyFirstProgram_v1.cpp program
downloaded to your disk area in part C.

Visual Studio is a sophisticated but easy to use integrated development environment (IDE) for C++
(and may other languages!) You will see that this environment recognizes C++ keywords and displays
them in color.

B.1. Starting the Visual Studio Environment (In Engineering Labs)


1. Click on Start in the task bar, and then move the mouse to All Programs.
2. When the sub menu appears, slide your mouse to the option called Engineering Applications.
3. Choose Microsoft Visual Studio 2005.
4. Again, choose Microsoft Visual Studio 2005. See figure below.

Page |1
B.2. Creating a Project in Visual Studio (to compile and run programs)
Even though we wish only to compile and run some fairly small and simple programs, we still need to
create a ‘project’. In general, this enables a programmer to go back later and open and modify the
source code of this project. Different programs (e.g. different labs) can be contained in different
projects. In Visual Studio 2005, a Solution can contain more than one project. In the following, let’s
create a solution called ES036Labs and a project called Lab00. When you want to work on your Lab01
next time, you can open the solution ES036Labs and add another project called Lab01.

To create a solution and a project:


1. Click File in the top menu bar of Visual Studio, and then select the New... option, and select
project (shown on the next page).

2. This will bring up a dialogue box that contains many project possibilities (shown below).
3. Click on “Win32” in the left pane of the window (shown below).
4. Select “Win32 Console Application” from the right pane of the window (you may need to
scroll down in the window to find this option) (shown below).

Page |2
5. Enter a name for the project in the lower part of the windows that says <Enter name> (see
above): Lab00.
6. Make sure that the location specified for the project is on drive H: If it is not, replace the
location with H:.
7. Now enter the name for the solution: ES036Labs.
8. Press OK to create the solution and the project.
9. A Win32 Application Wizard window will appear (see on the next page). Select “Application
Settings” on the left side of this window.
10. Under additional options, check the “Empty Project” box (see on the next page).
11. Select Finish (see on the next page).

Page |3
When we complete theis process, Visual Studio create a folder H:\ES036Labs. This is the folder where
the solution is located. You will see a file with extension .sln here (ES036Labs.sln). This is the file you
need to click if you want to reopen your solution next time.

Notice the folder called Lab00 is located in our solution folder. This is the project folder for our Lab00
and the source files (.cpp and .h) are saved inside this folder. If you have your files elsewhere (e.g your
USB pen or My Documents) you should copy them into this folder. When you create your Lab01
project next time, another project folder called Lab01 will be created in the solution folder. In your
Visual Studio window you will see a view like this at the top left in the following figure1.

1
If you do not see this view select View > Solution Explorer or click on the solution explorer button.

Page |4
When you have created your Lab01 next time, the view will look like this.

Now we see how Visual Studio helps us to manage our projects and files.

You wish to add some source code to this project:


We have two options when we want to add source code to our project. First is to create new file (Add
New Item) and the second is to use an existing item (Add Existing Item).
1. Copy the .cpp files that you downloaded from the WebCT to folder H:\ES036labs\Lab00
2. Click Project in the top menu bar of Visual Studio, and then select the Add Existing Item
option (see the following figure).

Page |5
3. Since we have already copied the files to the right place (H:\ES036labs\Lab00) you will see the
icon of the file you need to add. Select MyFirstProgram_v1.cpp. If you wish, by
selecting the arrow beside the Look in address box, you can navigate to the folder in which you
saved the MyFirstProgram_v1.cpp file on your network disk area (H:). To see the C++
files in this folder, set the Files of type (see bottom box of Look in menu) box to contain Visual
C++ Files (which may already be the default value).
4. You should now see the name of this file (MyFirstProgram_v1.cpp) displayed under the
folder Source Files in the upper left panel.
5. Double click on MyFirstProgram_v1.cpp under “Source Files” on the left-hand side to
display the contents of the file in the main portion of the Visual Studio development
environment.

To get the program working we need to do two important things. First we need to compile (and link)
the program. This takes the C++ statements that you have written and converts them to machine-code,
which the CPU can understand. The next step is to run the program. This will tell the CPU to follow
the machine-code instructions generated from the C++ code. Here is how to do that.

Compiling the C++ statements in the file MyFirstProgram_v1.cpp:


1. To compile, select the Build menu in the top menu bar (see the following figure).

Page |6
2. Select the option Build2 Lab00 (assuming that Lab00 is the name of your project).
3. A lower window will show the results of this process. (As it is, MyFirstProgram_v1.cpp
file will compile successfully.) You will see an output like this. You should have 0 error(s).

1>Lab00 - 0 error(s), 0 warning(s)


========== Build: 1 succeeded, 0 failed, 0 up-to-date, 0 skipped ==========

Running the program created by the above step:


4. To run (execute), select the Debug menu in the top menu bar.

5. Select the option Start Without Debugging.


2
Rebuild Solution will rebuild all the projects in the solution as defined by the configuration manager. If your projects are
small this is a convenient option.

Page |7
6. A black 'DOS-type' window will appear showing the output of your program.

To edit the source code (modify the program):


7. To edit, double click on MyFirstProgram_v1.cpp under the folder Source Files in the
upper left-hand panel if it is not already displayed in the main panel. This will load your file
into the main panel.

8. Modify the "Welcome to C++!!" data to read something a little different, say "Hello there!
Welcome to C++!!"

To recompile the source code of this project:


9. To recompile, select the Build menu in the top menu bar.
10. Select the option Rebuild Lab00 (assuming that Lab00 is the name of your project).
11. A lower window will show the results of this process.

To rerun the executable file created in the compilation process:


12. To run (execute), select the Debug menu in the top menu bar.
13. Select the option Start Without Debugging.
14. A black 'DOS-type' window appears showing the new output of your program.

B.3. Reviewing the Visual Studio Environment


Now let's try everything again from scratch!
1. Go to the ES036 web site and download to your network disk area (H:\ES036labs\Lab00) the
file ArithmeticOpearation.cpp.

Page |8
2. We need to remove the existing source file to add the new one. To do that right click on the
existing source file and select remove. Removing does not mean that you are deleting the file.
The file is just removed from the project. This step is important3.

3. Add the file ArithmeticOpearation.cpp as the source code.


4. Compile and run this code. Input (type and hit enter key) the integer values 4 and 2 and see the
output.

Modifying the File ArithmeticOpearation.cpp


5. Uncomment one ‘result’ statement at a time while others will be commented out and check the
output with integer values 4 and 2 respectively.
6. This time, try to use integer values 2 and 4 respectively and check the output for each case.
Find a reasonable answer, since the results are not same as the previous set
7. Try to uncomment the second cout statement and comment out the first one and then run the
program to check the output. In both cases, the output will be the same. This will give you an
idea of the endl function. Make a reasonable guess and be ready to respond to TA’s question
on this.

You can practice with a couple of more programs downloaded from the course web site!
Possible things to try:
a) If you modify a file with a deliberate error, you can see what sort of message this error
3
If you get an error saying
1>Arithmetic_Operations.obj : error LNK2005: _main already defined in MyFirstProgram_v1.obj
you have more than one source file containing a main function. Remove all except the one you want.

Page |9
generates when you try to recompile.
For example,
- try omitting a semicolon from the end of a statement (and then recompiling).
- try a different integer name
b) Can you start a new program from scratch? (use the editor to open a new file, then edit the file,
compile and run)

C. Creating a New Program from Scratch


After completing this section you will be able to create a new program in C++.
1. Remove existing source files. (If you do not already have a project, you need to create one. If
you followed this lab from the beginning, you should have a project called Lab00 within the
solution ES036Labs.)
2. Click File in the top menu bar of Visual Studio, and then select the Add New Item option.
3. Select C++ File

4. Type in the code in page 16 of the text book (listed below).


5. Replace the first set of comment lines with a set of comments that identifies you (name, student
number, section, email address). Compile the program and execute it.
6. Call your TA to your computer so that they can verify your result.
7. Modify the program (save it as a second file with a different name) so that the program also
prints out the perimeter of the triangle inscribed by side 1, side 2, and the hypotenuse.
8. Compile the program and execute
9. Call your TA to your computer so that they can verify your result.
10. Now save a copy of your modified cpp file giving your username as the file name. (For
example, for a student named Wes Tern, the account name is probably something like wtern).

P a g e | 10
Submit this file via WebCT.
/*********************************************************************
ES 036 - Summer 2006 – Lab 00 Part E

Name: Your Name


Student No.: 250 xxx xxx
Email:
Section:
Date:

This program computes the distance between two points

*********************************************************************/

#include <iostream>
#include <cmath>

using namespace std;

int main()
{
//Declare and initialize objects
double x1(1), y1(5), x2(4), y2(7),
side_1, side_2, distance;

//Compute the sides of the triangle


side_1 = x2 - x1;
side_2 = y2 - y1;
distance = sqrt(side_1*side_1 + side_2*side_2);

//Print the distance


cout << "The distance between the two points is "
<< distance << endl;
//Exit the program
return 0;
}

D. Logging Off After a Session


It is very important that you log off the machine after you have finished.
Otherwise, someone may do something malicious with your account. However,
DO NOT shut the computer off!
1. To log off, click on the Start button, and select Shut Down at the bottom of the menu..
2. The shutdown dialogue box will appear, and ask "What do you want the computer to do?" If
the choice box contains "Log off your_user_name" then click on OK. If the choice in the box is
"Shut down" or "Restart" then click on the down-arrow to the right of the choice, click on "Log
off your_user_name", and then click on OK. (This will end your session, leaving the computer
running for the next student.)

E. General Lab Instructions to Help Labs Run Smoothly


• Attending all the labs is mandatory.

P a g e | 11
• Obtain the lab instructions from the ES036 web site.
• Read through the lab instructions and work on the alb before coming to the lab.
• Write the QUIZ at the beginning of the lab.
• Answer any pre-lab questions.
• Bring a printed copy of the lab handout to the lab. It will make it easier to follow the
instructions.
• Preferably have the lecture notes printed with you.
• Retain copies of the cpp files that you create or submit via WebCT (have a back up of your
ES036Labs folder).

P a g e | 12

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy