G. Reading and testing simple C++ program

advertisement
CS106 — 9/06/02 — LAB #1
OOP! WELCOME TO MCS AND VCPP!
The purpose of this lab is to introduce you to the computer systems you will be using in this course. Most of these will
be on the MCS network, which is accessed through Skidmore's network. You can do this on most machines across
campus. Follow the steps indicated below, and save this lab sheet as a reference. You should each work at a
machine on your own, but please don’t hesitate to talk to each other; in particular, if something below doesn’t happen
the way I say it will, ask your neighbor for help. If you still have trouble, of course ask me, but lab days are an
opportunity to learn mostly on your own and from each other. Note: there are lots of different ways to do things in
Windows 95 and Visual C++; I've tried to choose easy-to-do-and-remember ways, but feel free to let me know if you
think you have a better way to do something.
A. Logging into the MCS1 Network on a PC
1. If your machine is off, turn it on! Once a login screen is showing, enter your username and password and press
Enter. If the computer is on but there is not a login screen showing, press Start at the bottom left to logout the
previous user – then a login screen will eventually appear.
2. The next step after logging in to get connected to the MCS network. To do this, double-click on the icon that says
MCS1 Network (away from Harder 201, click on Applications, etc.) A list of icons will appear – double-click the
bottom one, which says User SETUP—RUN ME. This attaches you to the MCS1 network and gives you access
to folders and program appropriate to the MA, MC, and CS courses that you’re taking.

This process has logged you into the Mcs1 network, and you should now be looking at the Mcs1 Network folder.
This folder contains most of the programs and sub-folders that you’ll need in this course. For our purposes, the
most important subfolders here are Handout, where your MA/CS teachers supply files to their students, Submit,
where students submit files to their MA/CS teachers, and Compilers, which contains the Visual C++ compiler.
B. The Handout \CS106a folder
1. In the Mcs1 Network folder, double-click the icon Handout. You should see a list of folders for each of the various
MA and CS classes being offered this semester. Our class has folder CS106a – double-click that folder. Now you
see a list of folders with files I have created for you to look at and/or copy: HWFiles will contain all homework
assignments; VCPPFiles will contain all our program files; Slides will contain all the Powerpoint files I use in
class.
C. Using Visual C++ and creating a Project Shell
In this course all our C++ programs will be saved in files whose names have the extension .cpp. In order to run a
C++ program using the VCPP (Visual C++) software, it has to be made part of what's called a project. This is a
folder that contains not just the C++ program, but also other files that VCPP needs in order to run the program.
Unfortunately, project folders can take up a lot of space, so in this course we will generally use just one project folder,
and we will insert and delete C++ programs from it. In the next steps you will create your own ProjShell folder.
1. From the MCS1 Network folder, double-click Compilers, then Microsoft Visual Studio 6.0, then Microsoft
Visual C++ 6.0. This starts up the Visual C++ PDE (PDE stands for Program Development Environment), where
we can create, compile, run, debug, and modify programs. If there are any introductory windows (like "Welcome",
or "Tip of the Day"), close them.
Page 1 of 4
2. Click on File from the menu at top, and then New. This brings up a screen which offers you a variety of “new”
things to create. We want to create a new project, so be sure the Project Tab is selected. From its choices, click
on Win32 Console Application.
3. You must fill in the boxes labeled Location: and Project Name: -- preferably in that order. In Location: type
\\datastor\students\<your-class>\<your-user-name>. In Project Name: type ProjShell, and then click OK. This
should bring up another window (if it brings up an error message, call me over). Empty Project is what we want,
so make sure that’s selected, and then click Finish, and then OK.
4. Before going further, you must change one project setting to save your limited network disk space. From the
menu list at the top of the screen, select Project, and then Settings. This should open a window in which you
should see the work Debug in two different places. In both places, change this to c:\Debug. Press OK after
you’ve made the two changes.
The Debug folder is a folder where the Visual C++ system stores some files it creates when you compile a program.
These files can be quite large. Whenever you are looking at folders and come upon a Debug folder, feel free to
delete it! It will be recreated next time you compile, and this way you’ll delete old files from previously compiled
programs.
5. Now let’s look at the VCPP (Visual C-Plus-Plus) development windows. You should see three windows: one
along the bottom, and two side-by-side above it – if you don’t see three windows, call me over. The left-hand one
shows you various “views,” i.e., information about ProjShell. We’ll be most interested in the FileView tab, so
select that. If you click the + sign now, next to ProjShell Files, you’ll see 3 folders, which will contain the various
types of files the ProjShell might contain. For now they’re all empty, since we haven’t yet added anything to this
empty project. We’ll discuss the other two windows in part D.
D. Adding a new source file to ProjShell
1. We’re now going create a new C++ source file, i.e., a program, and add it to ProjectShell. Actually, we add an
empty file first, and then type stuff into it. From the menu at top, select Project—Add To Project—New—C++
Source File. Type in a name for this file in the box provided, calling it Lab01D1 (meaning Lab 01, step D1), and
press OK. This adds a file named Lab01D1.cpp to ProjShell, and opens it in the editing window, which is the
large window at the right. Here you can edit any text or C++ file, even if it’s not part of ProjShell. In fact, it’s easy
to make the mistake of assuming that a file you’ve opened in the editing window is part of the project, but it need
not be. Only the files listed in the left-hand FileView window are part of the project – check there now to make
sure that Lab01D1.cpp is part of ProjShell, by clicking on the plus-sign next to Source Files.
2. Now, click in the editing window, because you are going to add some text to this file. Copy as exactly as possible
the following lines, replacing *** with your name. This is a very short, but complete, C++ program. The editing
window helps you by displaying in blue words that are part of the C++ language. This program should also have
comments, giving the file name, author, date, and description, but we’ll add those in shortly.
#include <iostream>
using namespace std;
void main()
{
cout << "My name is ***." << endl;
}
3. From the Build menu at top, select Rebuild All, which compiles the program and links it with other necessary
files to product an executable file – one that we can run. If you’ve typed in the program correctly, the Build screen
at the bottom should say there are 0 errors and 0 warnings. If not, you need to find and correct your typing
error(s), and Rebuild All again.
4. Once you have no errors, select Build-Execute ProjShell.exe (some of the symbols next to the menu choices
also appear as buttons on the toolbar – there are usually several ways to do a given task). This should run the
Page 2 of 4
program and should display a “DOS screen” containing the output. Pressing any key closes the DOS screen.
E. Adding comments, correcting errors
5. A revised version of this program, shown below, has descriptive comments added, indicated by two slashes //;
this symbol instructs VCPP to ignore anything that follows on the line. It also has an error, namely, I changed the
double-quote after the name (from the original program) to a single-quote. Add the new text to the existing
program (it’s shown in boldface) – don’t forget to change the double-quote to a single-quote.
//Filename: Lab01D1.cpp
//Description: Simple first program
// to try out VCPP compiler.
//Author: ***
//Date: 9/06/02
#include <iostream>
using namespace std;
void main()
{
cout << "My name is ***.’ << endl;
}
6. Now Build-Rebuild All the revised program. There should be two errors (more if you have typing errors). C++
will sometimes generate multiple error messages for a single error, since the error causes it to misinterpret your
code. VCPP provides various tools to help you find your errors. The simplest of these is the function key F4. If
you press this key once, it points you to the program line containing the first error, as well as highlighting the error
description in the bottom window. Notice in this case that the description seems to bear no relation to the error.
That is often the case, again because of VCPP’s inability to interpret your code. However it does usually do a
pretty good job of at least pointing at the right line – which it should be doing in this case. Without correcting the
error, press F4 again, and it should point to the next line, but give a different error message. This is because the
error also made it unable to parse the next line. Press F4 twice again to return to the first error.
7. Now correct the error by changing the single quote back to a double-quote. Rebuild All again and Execute
again, to check that the program runs properly.
F. Cleaning and Removing a program from a project
1. Select File-Save. This saves the file showing in the editing window (Lab01D1.cpp), not the project workspace.
Since we will use the single project ProjShell for almost all the projects we write, we’ll need to delete programs
from the project when we’re done with it (this does not delete it from the disk if we’ve saved it). This is a two-step
process: first, you clean the Debug folder to remove files associated with this program, second, you delete the
program from the project. You must clean first before deleting for the clean step to work. We do this in the next
two steps.
2. To clean the Debug folder, select from the menu the choices Build—Clean. This removes any files in the Debug
folder associated with this program.
3. Now remove this file from the project ProjShell (remember that we will insert and delete all our C++ programs
from ProjShell). You can do this by simply pressing the plus sign next to ProjShell Files in the left-hand window,
highlighting Lab01D1.cpp, and pressing Delete. It is no longer part of the project. To remove it from the editing
window, click the small x at the upper right of the editing window (not the one all the way at the upper right, which
closes VCPP).
G. Reading and testing simple C++ program
1.
Read through the following C++ code and write down (a) what values you think the variables a, b, Sum, Average
take on during the course of the program, and (b) precisely as possible, what you think the output will be.
Page 3 of 4
#include <iostream>
using namespace std;
void main()
{
//Define and initialize object
float a = 3;
float b = 4;
//Calculate results
float Sum = a + b;
float Average = Sum / 2;
//Display results
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "Sum = " << Sum << endl;
cout << "Average = " << Average << endl;
}
2. I have put a copy of this program in the Mcs1\Handout\CS106a\VCPPFiles folder, under the name
Lab01G1.cpp. You will not be able to compile that copy, so first copy this file to your own folder (minimizing
VCPP for the moment). Do this by opening the folder Mcs1\Handout\CS106a\VCPPFiles, clicking on the file
Lab01G1.cpp to highlight it, and then selecting the menu option Edit—Copy. Now navigate to your own folder
(\\datastor\etc.) and select the menu option Edit—Paste; if you know how and you’d like to, you can create a
Lab01 subfolder and put it there.
3. Now, going back to VCPP, to add your copy of the program to ProjShell, select from the menu Project—Add to
Project—Files. In the “Look in:” box, you can click on the down-arrow button to get a list of folders, and work
your way to your folder. If you have trouble navigating, call me over.
4. Inserting it into the project does not open it in the editing window, so go to the FileView window on the left, and
double-click the file to open it in the editing window.
5. Add comment lines at the top as we did in the last program (but appropriate to this program). Refer back to step
E5 for the general format.
6. Now build and run the program to see if its actual output fits with your prediction in Step 1.
H. Finishing up
1. When you’re all done, you can “fool around” a bit if you like, making changes to the program to see what
happens. When you’re really all done, perform Build – Clean, close the Visual C++ program, and then log out
as follows: Press the Start button at the lower left of the screen, and then select Log Off. When you’re done, the
machine will be ready with a login screen for the next user (your username will be in it, but that’s not a problem,
since your password is not!). You are not permitted to use Shut Down on public machines, so don’t worry about
turning off your machine.
Well I hope that went smoothly (at least more or less) for all of you. It may seem like quite a maze of buttons to press,
but don’t worry. You won’t really need to memorize them, because they’ll all become very familiar as we use them in
class, and you use them in labs and on homework. Congrats on making through your first CS106 lab – see you in
Harder 201 on Monday!
Page 4 of 4
Download