Lab 0: Getting Started with C++


Introduction

The main purpose of this lab is to introduce you to the computing environment of your laboratory. You will use the ideas in this lab again and again throughout this course, so you should make every effort to understand not only what, but why you are doing what you are doing at each step.

Before we can begin our session, your instructor must inform you how to begin a session with the computer at your particular institution. The way this is done differs from school to school, according to the kind of computer being used, whether they are networked or stand-alone, whether a security system is in place, and so on. Among the things your instructor will tell you will be the answers to the following questions:

  1. Should the computer be turned on at the beginning of the exercise and off at the end of the exercise, or does it remain on all of the time?
  2. Do users of the computer have personal accounts (requiring one to login to begin an exercise), or can anyone use the computer?
  3. If users have personal accounts:
    1. How do I find out my username (the name by which I am known to the computer)?
    2. How do I find out my password (the secret word that allows access to my account)?
    3. Is it necessary to change my password (and if so, how is it done)?
  4. In your windowing environment (Windows-95, Windows-NT, etc.):
    1. Must I do something special to enter that environment?
    2. What role does the mouse play in that environment?
    3. How do I exit that environment?
  5. What must I do to quit a session using the computer?

About this Manual

In this (and every other) exercise, instructions will be printed in this default font (the one you are reading). To help you distinguish the instructions from what appears on your screen, text that you should see displayed on your screen will be shown in this font. Text that you are to type or enter will be shown in the this font.

The distinction between typing something and entering something is as follows:

Getting Started with Windows-95

Follow your instructor's instructions for beginning a session. When you see a white arrow on the screen (pointing to about the 11-oclock position), the machine is ready for you to begin. If you have not used a mouse before, move it around on its pad and note how the white arrow moves in response. The white arrow is called the mouse cursor, and it is used to point at objects on the screen.

If you are not accustomed to using a mouse, take a few moments to practice pointing at objects on the screen.

Interacting with the Computer

The way in which you interact with a computer depends upon the computing environment of its operating system. For example, an environment in which you use a mouse to interact with menus, windows, and icons on a computer's screen is called a graphical user interface, or GUI (pronounced gooey) environment. Windows-95 and Windows-NT both provide a similar GUI environment.

Since you are reading this, you are presumably doing so using a software application called a web browser, such as Netscape or Internet Explorer. A web browser is an application that provides a GUI environment for exploring the World Wide Web, a network of computers from around the world linked together to share information. As you have (presumably) already discovered, a web browser lets you download information from remote computers simply by pointing the mouse at a link and clicking its left button.

In contrast to a GUI, there are environments in which you repeatedly:

  1. are prompted for a command,
  2. enter a command, and
  3. the machine displays the result of the command.
These are called command-line environments -- environments in which you interact with the machine by entering commands following the system prompt. The MS-DOS operating system (a precursor of Windows-95 and Windows-NT) uses a command-line environment.

In order to use a command-line environment, you must learn those commands that the environment "understands." This in general makes a command-line environment more difficult to use than a GUI, since you must be able to recall the right commands to use the system. By contrast, users of a GUI environment need only be able to recognize the proper menu choice, link, or icon they need to select in order to make something happen.

Let's begin by learning about the mouse. The left mouse button is usually used to select and open an object on the screen. Selecting an object is accomplished by pointing the mouse at the object and clicking once, opening is is similar, but requires a double-click (clicking the left button twice in rapid succession).

To illustrate, there is probably a tiny picture (called an icon) of a single computer labeled My Computer in the upper left corner of your computer's screen. (This is its default name and location -- depending on your local configuration, its name or position may be different.) Point the mouse at that icon and click the left mouse button. The icon should darken, indicating it has been selected.

Next, double-click while pointing at the My Computer icon, to open your computer. A new window should appear listing some of the storage devices that are inside your computer (e.g., a floppy drive named A:, a hard disk named C:, a CD-ROM named D: are common devices).

To select your browser window, just move the mouse over your browser and click the left mouse button. Note that the titlebar at the top of the window darkens (i.e., it turns blue on most machines) to indicate that the window is selected. If you click the left mouse button while pointing at your My Computer window, its titlebar darkens accordingly. Since you may have multiple windows on your screen at the same time, Windows darkens the titlebar of whichever window is currently selected, which is known as the active window.

Window Operations with the Left Mouse Button

One of the most common things people want to do with windows is reposition them on the screen. This is quite easy in Windows-95: just point the mouse at the titlebar of the window, hold down the left mouse button, and move the mouse until the window is where you want it. This is called dragging the window. Then release the left mouse button and the window will stay put (until you drag it somewhere else). This is called dropping the window. Dragging and dropping makes it easy to reposition a window.

Another common window-related task is resizing a window. Again, Windows-95 makes this easy: just point the mouse at one of the corners of the window until the mouse cursor changes from an white arrow to a diagonal two-headed arrow (called the resize cursor). Then drag the corner of the window in or out until it is the size you want. By dragging, dropping, and resizing you can arrange the objects on your screen according to your personal tastes.

In the upper right corner of every window are three buttons: one labeled with _, one labeled with a tiny window, and one labeled with an X. Click the _ button of your My Computer window, and you should see the window "collapse" into the task bar at the bottom or your screen. If you left-click on the rectangle labeled My Computer, the window will reappear. The _ button is sometimes called the minimize button, because clicking it shrinks the window until it is just an icon in the task bar at the bottom of the screen. Selecting that icon on the task bar returns the window to its previous position and size. Take a moment to try it with the My Computer window.

The button with the tiny window is the maximize button. Clicking it causes your window to enlarge and fill the entire screen, at which point the button changes to show two tiny windows. Clicking this button returns the window to its original size and position. Take a moment and try it with the My Computer window.

An alternative way to maximize a window is to double-click in its titlebar. Double-clicking in the titlebar of a maximized window returns it to its original size and position.

The final button in the upper right corner of a window is the X button which is called the close button. Clicking this button removes the window from your screen, terminating any program that was running within the window. You should use this button with caution, since any unsaved work (see below) is lost when a window is closed.

The Right Mouse Button

The right mouse button offers different options from the left button, usually allowing you to pull down a menu of options for the object at which the mouse is pointing. If you click it over your browser, you should see a menu with some choices, if you click it over your screen's background, you should see a menu with different choices. On this latter menu, note that some of the choices have a tiny arrow to their right -- if you move the mouse over that arrow, a secondary menu appears with more choices.

We will occaisionally use the right mouse button, but the left mouse button is used far more often. As a result, if we say "click the mouse button" without specifying which one, we will be referring to the left button. If we want you to click the right button, we will explicitly say so.

Customizing Your Computer's Interface

Before we explore, lets take a moment to make sure that your environment is configured in consistently with this exercise. While the customization in Windows-95 is convenient and user-friendly, covering every possible configuration is difficult and inefficient, so we will set up your environment to have a uniform look and feel.

Open (i.e., double-click) your My Computer icon. Then click on the View menu and make certain that selections Toolbar and Status bar both have check-marks beside them. If either of them does not, select it with the mouse, so that both of these features are enabled.

Next, select View -> Details to provide a detailed listing of the devices in your computer. This will provide more information than the other choices, including how much free space is available on each device.

Finally, select View -> Options and an Options dialog box will appear. Make sure the the File tab is selected in the box, and then make sure that the second radio button is selected:

Browse folders by using a single window that changes as you open each folder.
This will help to avoid "screen clutter" by using a single window for navigation through your computer.

Navigating in Your Computer

Ok! We're ready to explore! If you have a floppy disk, insert it into your computer's floppy drive, and let's get started.

At the top of the list of devices in the My Computer window should be 3-1/2 Floppy (A:) which refers to your floppy drive. Double-click on its icon or name and the window will change to display the contents of whatever disk you placed in the drive. Note that the title of the window changes to A:\, which is the short-name by which the system refers to your floppy drive.

To get "back" to your My Computer window, click on the small button that looks like a folder with an up-arrow inside it. This button always take you "up" to the folder containing the current window, and since A:\ is "inside" My Computer, clicking this button returns you from A:\ to My Computer.

The other icons in your My Computer window refer to the other storage devices in your computer. If your computer has a second floppy drive, its name is B:. Since A: and B: are commonly used for floppy drives, C: is the name of the hard disk on most Windows-95 computers, D: is the name of either a second hard disk or the CD-ROM drive. Your computer may have additional devices (e.g., network drives) depending on how it has been set up.

Each of these other devices can be explored in exactly the same way as your floppy drive -- by clicking on the icon for that device, and then in its window, clicking on whatever icon(s) you want to examine next.

Open your C: drive, and find the Temp folder, and open (i.e., double-click) it.

Folders in Folders

Very soon, we'll be entering and running a simple C++ program. But when we are done, we'd like to be able to save your program somewhere "safe" -- somewhere that, if the power goes off, your program can still be retrieved. To save your program safely, the computer stores it on magnetic media (usually a hard disk) in a container called a file.

During this course, we'll create dozens of files, and so some means of organizing them is needed. Just as the documents in a filing cabinet are often kept in manila folders, Windows-95 lets us create a folder container in which we can store related files.

To do so, point the mouse at your C:\Temp window and click the right mouse button to display a menu. Move the mouse downwards until it points at the New choice, and wait while the secondary menu appears. Then move the mouse again until you are pointing at the Folder choice, and click either button. You should see a folder icon labeled New Folder appear on your C:\Temp window.

When the new folder appears its name is automatically selected, so that you can give it a more descriptive name. Since we are creating a folder in which to store our labs, type labs to rename this folder appropriately.

Viewing the Contents of a Folder

To see what's inside of a folder is easy -- just double-click on the folder icon to open it! Take a moment to look inside your labs folder. It should be empty, since we have not yet put anything into it. Note that the titlebar of the window changes when you open a folder, to indicate "where you are" within the folder hierarchy.

As this indicates, the labs folder is within the C:\Temp folder. Now, suppose we want to get from C:\Temp\labs back to C:? We could click twice on the button with the folder-and-up-arrow icon, but when you want to jump "up" several folders at once, the white text window containing labs provides a faster way: Click on this box and a drop-down menu will appear showing that labs is within Temp which is within C:. Just click on the C: choice to return directly to C:.

For practice, navigate back to C:\Temp and make three other folders: one named "practice", one named "projects" and one named "myLib".

Discarding a Folder

We made practice just for practice, to learn how to get rid of unwanted folders. Click on practice, drag it to the Recycle Bin icon (the icon will darken when the mouse is properly positioned over it) and drop it there. Your folder should disappear "into" the Recycle Bin, and if the Recycle Bin was empty, its icon should change to indicate that it is no longer empty.

If you throw something away and later decide you want to get it back, you can open the Recycle Bin by double-clicking on it and drag what you want to retrieve from the window back to your desktop. If you are certain that you will not want to retrieve anything from the Recycle Bin, you can empty the bin by pointing the mouse at the Recycle Bin icon, clicking the right mouse button to get its menu, and choosing the Empty Recycle Bin choice from the menu. This operation cannot be undone, so use it with caution.

Your First Program

Once you have created your C:\Temp\labs folder, you are ready to write your first C++ program. For now, writing a program consists of two steps:
  1. Writing a source program in a high-level language (i.e., C++) and storing that program in a file; and
  2. Translating that source program into a machine-language program (i.e., a binary executable).
Visual C++ uses what is called an integrated desktop environment (IDE) to conveniently perform both of these steps (and other tasks). More precisely, the Visual C++ IDE provides a text editor window in which you can enter the text of a program, and then save that program in a file. Once your program is saved, you can translate it into your computer's machine language (the binary 0s and 1s that a computer "understands") using the Visual C++ compiler and linker. Graphically the process can be pictured as follows:

As we shall see in a later lab exercise, this is a bit of a simplification, but it is sufficiently accurate to give you an idea of what is occuring. For now, just realize that the IDE's text editor is what you use to enter a program, and its compiler and linker are what you use to translate it.

Starting Visual C++

To get started, invoke Visual C++ according to your instructor's directions. (This may involve double-clicking on an icon on your computer's desktop, or clicking on the Start menu and following the submenus through Programs -> Microsoft Visual C++ 5.0 -> Microsoft Visual C++ 5.0). A window should appear labeled Microsoft Developer Studio. If you are viewing these instructions through a web browser, you will want to use the "un-maximize" button to resize the window that appears so that you can view both it and your web browser simultaneously. If a "tip of the day" window is displayed, close it and continue.

Our first program will input a base-10 integer and display that same value in base-10 (decimal), base-8 (octal) and base-16 (hexadecimal). We will thus name the executable program bases.exe, and name the source program bases.cpp (C++ source programs end in .cpp by convention, and executable programs end in .exe in Windows-95.)

Creating a Project

The Visual C++ IDE provides a convenient feature called a project for simplifying the translation of a source program into machine language. The normal procedure is to create such a project first, before we create any source files for our program.

To create a project, move the mouse within the Microsoft Developer Studio window and click on the File menu, and choose New from the menu that appears. This should cause a window to appear labelled New, containing "tabs" labelled Files, Projects, Workspaces, and Other Documents. Make sure that the Projects tab is selected, and then click on the Win32 Console Application choice. (Each of the exercises in this manual will use this same kind of project.)

First fill in the text box labelled Location: by clicking the ... button to the right of the box. In the window that appears, navigate to the C: drive, the TEMP folder there, and your labs folder within TEMP.

Then fill in the name of the project, which should always be the same as the name of our program. Since our program is to be called bases, type bases in the text box labelled Project Name:. Note that as you type, the name bases is also appended to the location C:\Temp\labs. Visual C++ will automatically create a folder named bases in which to store the work you do for this project. Click the Ok window and the Microsoft Developers Window should reappear, with bases prepended to its title.

Creating the Source Program

Next, we want to create a new file named bases.cpp in which to store our program. To do so, move the mouse to the File menu within the Microsoft Developers Studio window and choose File -> New again. The same New window will appear as before. This time, make sure that the Files tab is selected, then choose C++ Source File from the list of options presented.

In the text box labelled File Name:, enter bases.cpp, and then click the Ok button. A subwindow titled bases.cpp will then appear within the Microsoft Developers Studio window, in which we can type our program.

Within the bases.cpp subwindow, enter (and personalize) the following C++ source program:

/* bases.cpp demonstrates basic I/O in C++.
 *
 * Author: Jane Doe.
 * Date: 2/29/99.
 * Purpose: Lab 0 in CS-1 at the University of Gallifrey.
 *
 * Specification:
 *   Input(keyboard): aNumber, an integer;
 *   Output(screen): the base 10, 8 and 16 representations of aNumber.
 ***********************************************************************/

#include <iostream>
using namespace std;

int main()
{
   // 0. print a message explaining the purpose of the program.
   cout << "\nThis program inputs a base-10 integer"
        << "\n\tand displays its value in bases 8 and 16\n";

   // 1a. ask the user to enter an integer.
   cout << "\nPlease enter an integer: ";
   // 1b.declare an integer container to hold the input number
   int aNumber;
   // 1c. input an integer, storing it in variable aNumber.
   cin >> aNumber;

   // 2. output the base-8 and base-16 representations of aNumber.
   cout << "\n\nThe base-8 representation of " << aNumber << " is "
        << oct << aNumber
        << ",\n\tand the base-16 representation is "
        << hex << aNumber
        << "\n\n";

   // 3. terminate normally
   return 0;
}
One shortcut is to copy-and-paste the text above from your browser's window into the bases.cpp window. Let's learn how to do this next.

In Windows-95, a block of text can be selected by pointing the mouse at the top of the block of text, and dragging downward to the bottom of the block of text. To copy-and-paste the selected text:

  1. Choose Edit -> Copy from the Edit menu of the window in which you have the text selected (i.e., your browser). You can alternatively use the keyboard shortcut Ctrl-c (hold down the Ctrl key and type c) to copy whatever you have selected.
  2. Move the mouse to the window in which you want to paste the text, and position the mouse cursor (i.e., click) where you want the text to be inserted.
  3. Choose Edit -> Paste from the Edit menu of the window in which you want to paste. You can alternatively use the keyboard shortcut Ctrl-v to paste whatever you most recently copied.

Using this approach, copy the program shown above into the bases.cpp subwindow.

Personalizing the Program

As given above, bases.cpp is the work of a fictitious person (Jane Doe) on a fictitious date (February 29, 1999), in a fictitious course (CS-1) at a fictitious university (the University of Gallifrey). Edit the program's opening comment (the part between the /* and */ symbols) as appropriate to make it your work on the current date, in your course at your university.

As you've probably figured out, you can use the mouse to position the cursor at an arbitrary point by pointing at that point and clicking the left mouse button. The arrow keys (or the mouse) can be used to reposition the cursor.

If you mistype something, it can be erased using the delete (or backspace) key. On many keyboards, the Delete key is set up to erase whatever is to the right of the cursor, while backspace erases whatever is to the cursor's left.

If you make a mistake, you can always undo your most recent typing using the undo command: Ctrl-z or by using the Edit -> Undo menu choice.

Saving Your Work.

When the bases.cpp subwindow contains the bases.cpp program, you can store this program in a file by choosing File -> Save or by using the keyboard shortcut Ctrl-s. If you look in the folder C:\Temp\labs\bases, you will see that it is no longer empty, but now contains bases.cpp, bases.dsp, bases.dsw, bases.ncb, and bases.opt. Don't worry about these other files for the moment -- they are just your project and other administrative files Visual C++ uses to keep track of what is where.

Translating Your Program

When your source program is entered and saved, it is time to translate it into the computer's machine language. In Visual C++, this can be done using the Build -> Build bases.exe menu choice, or the keyboard shortcut F7 (function key F7). When this is done, Visual C++ opens the Build subwindow at the bottom of the Microsoft Developers Studio window and displays the result of the translation there. If all is well, you should see these messages appear:
Compiling...
bases.cpp
Linking...

bases.exe - 0 error(s), 0 warning(s)
If you see anything else, then your program contains typographical errors which you must correct before proceeding.

When your program contains errors, the Build subwindow lists them for you. If you double-click on a given error message, Visual C++ will move the cursor to the line in bases.cpp where it discovered the error. Compare that line and its surround lines with those of the source program in your web browser and fix any discrepencies. Then re-translate your program. If you still have errors, continue this edit-translate cycle as many times as necessary until your program is error-free.

Running Your Program

Running program bases.exe is just as easy as anything else in Visual C++. Choose Build -> Execute bases.exe or use the keyboard shortcut Ctrl-F5 and the IDE will run the program for you.

When it prompts you for input, enter an easy-to-check number (like 15, 16 or 17) and verify that you get the correct results. When you see the message

   Press any key to continue
don't press a key -- instead, continue to the next part of the exercise.

Printing a Hard Copy of Your Program

A paper copy of electronic information is called a hard copy. It is often useful to have a hard copy of one's source program, which is in the file bases.cpp. As with much of Visual C++, this is quite easy: just make sure that bases.cpp is selected choose File -> Print, or use the keyboard shortcut Ctrl-p. If you must do anything beyond this (such as specify a printer), your instructor will inform you of the details.

Printing an Execution Trace

It is often useful to be able to print a hard copy of the output from your program, especially for more complicated programs. Unfortunately, Visual C++ does not make this particularly easy. If you examine the bases.exe window, you'll note that there is no File menu or other means of printing its contents.

This forces us in a different direction. What we will do is (i) make a copy of our bases.exe window, (ii) paste that copy into a different application, and (iii) print the copy from that application.

For the first step, first make sure that the bases.exe window is selected, and then press Alt-Print Screen (hold down the Alt key and then press the Print Screen key). This copies the active window to a special system file called the clipboard.

To access that copy, we need a different program. Go to the Start menu (at the bottom of the screen) and choose Start -> Programs -> Accessories -> Paint. This will launch a new application called Paint. In the window that appears, use Edit -> Paste or Ctrl-v to paste what is on the clipboard (the image of your program's output) into the Paint window.

Once the copy of your bases.exe window appears there, you can use File -> Print (or Ctrl-p) to print the copy from within the Paint application.

Applying the Scientific Method

An important part of any science, including the science of computing, is to be able to observe behavior, form hypotheses, and then design and carry out experiments to test your hypotheses. The next part of this exercise involves applying the scientific method to infer (from the statements within bases.cpp) how the certain aspects of C++ output system work. Since two heads are (sometimes) better than one, feel free to work through this section with the person sitting next to you.

Observe

Study your hard copies of bases.cpp and bases.exe, positioning the pieces of paper so that you can see both of them simultaneously, side by side. Note in particular the points at which lines end and blank lines appear. Then study the text of bases.cpp and try to identify what within the program causes the output lines of text to break to a new line where they do.

Hypothesis

Construct a hypothesis (i.e., a statement) that states how you think output text can be made to begin on a new line in a C++ program.

Experiment

Design an experiment using bases.cpp that tests whether or not your hypothesis is false. (Recall that the scientific method can only prove that a hypothesis is false; it can never absolutely prove a hypothesis to be true.)

Then modify bases.cpp as necessary to perform your experiment, re-translate bases.cpp into machine language, and run it. If the resulting behavior indicates that your hypothesis is false, repeat the preceding Observe-Hypothesis-Experiment steps until you form a hypothesis that you cannot prove to be false.

When you are unable to prove your experimental hypothesis to be false, print a hard copy of your modified bases.cpp. On that hard copy, write down your hypothesis, your experiment, and circle that part of the program that performs your experiment. Then print a hard copy of its output (using the Paint application) and note on it the effects of your experiment.

Wrapping Up the Session

In the remainder of this exercise, we examine the final things you will need to do in most sessions.

Cleaning Up Your Project

A program consists of source files and one or more libraries. In the course of writing and debugging a program, it may be necessary to compile the program dozens of times! Since all debugging is confined to the source files, recompiling the (unchanged) libraries each time would waste lots of time.

To avoid this inefficiency, once a file is compiled, a Visual C++ project stores a copy of the compiled version called an object file. The Build -> Execute ... command then recompiles any files that have been changed, and uses the existing object files of any files that have not been changed. By avoiding unnecessary recompilations, this saves lots of time.

Object files speed every Build -> Execute ... after the first one, but they also consume disk space. To save disk space, the object files should be removed from a project any time you are done working on it.

To remove these files, choose Build -> Clean, and Visual C++ will display

   Deleting intermediate files and output files for project 'bases - Win32 Debug'.
in the Build message window at the bottom of the screen.

Quitting Visual C++

To quit Visual C++, just click on the close box of the Microsoft Developers Studio window.

Cleaning Up Your Directory

The various files you have created take up valuable disk space -- as it stands, bases will not fit on a floppy disk -- and so we will remove the non-essential files from bases. So long as we keep our source program and the project files, we can easily rebuild bases.exe from within Visual C++.

Navigate back to C:\Temp\labs and select (without opening) bases. Then go to the File menu and choose File -> Properties and a box will appear telling you (among other things) how much disk space bases consumes (4.51 Mbytes on our system).

Next, open bases and then drag the Debug folder to the Recycle Bin. (The Debug folder is where your bases.exe is stored, which as a binary, is quite large.) Empty the Recycle Bin and then navigate back to C:\Temp\labs. There, select (again, without opening) bases and see how big it is now that we have deleted Debug. On our system, bases is now just 87 Kbytes -- the Debug folder was consuming about 98% of the space!

Since a floppy disk only stores 1.4 Mbytes, the morale of the story is, always delete the Debug folder before trying to save your work to a floppy disk.

Copying Your Work to a Floppy Disk

If you brought a floppy disk with you, you can save a copy of your work by navigating to the C:\Temp folder, selecting your labs folder, and using Ctrl-c to copy labs. Then navigate to your floppy drive A: and use Ctrl-v to paste there. This will copy labs and everything in it to your floppy disk. When you drop the folders, you should hear the drive motor activate and then stop as the copy is performed. Note that this truly is a copy operation -- the original folder remains in C:\Temp. To avoid having someone else plagiarize your work, you should then drag C:\Temp\labs to the Recycle Bin, and then empty the bin.

Once you have cleaned up in this manner, end your session with the computer, following your instructor's instructions.

Note: If your head feels ready to explode, don't panic! This first lab covers a great deal of material, that you will use over and over again, and as you do so, you will begin to naturally memorize those commands that you use most frequently. You can speed up the process by reviewing each of the steps you took in this exercise and practicing in your free time.

Submit:

Your original hard copy of bases.cpp, the hard copy of its output, a hard copy of your modified bases.cpp annotated with the details of your experiment, and the hard copy of its output annotated as described in the exercise.

Phrases you should now understand:

Integrated Desktop Environment, Folder, File, Navigate, Editor, Compiler and Linker, Hard Copy, Printer.


Back to This Lab's Home Page

Back to the Prelab Questions

Forward to the Homework Projects


Copyright 1998 by Joel C. Adams. All rights reserved.