Lab 1

Pre-lab Homework:  Read the Computer Science Department's Style Guide.

Late policy (pre-lab homework): Pre-lab homework will receive a grade of 0 if not submitted at the beginning of the lab period. See the specific lab descriptions via the course web page for details. 

Late policy (labs): Any lab not completed and shown to your instructor during the lab period must be submitted (as a screen capture printout of your working program and paper copy of your source code) by the next class meeting or will receive a grade of 0.

Part 0: Lab Ground Rules

·         Our labs are available for your use during the day as well as after hours. If you would like to use a machine in a lab while the lab is in use by an instructor, you must first request permission to use the lab from the instructor.  Please do so without disturbing the class in session. 

·         No eating is allowed in the lab.

·         Drinking is allowed in the lab ONLY from a closable container.  A soda can is NOT closable.

·         Always log out when you leave the lab.

·         Do not attempt to physically alter any equipment in the lab.

·         If you are the last person leaving the lab, ensure the door is shut and locked.

Part 1: The Computing Environment

Login: Windows XP

·         Press Ctrl-Alt-Delete to start the log-in process.

·         Enter your Bancroft Hall user name and password, ensure the “Log on to” drop-down is set to USNA.

Your PC Account

·         Double-click on the My Computer icon.

·         The icons in the My Computer window are the "devices" associated with the computer you're logged-into. There are some disk drives (e.g, M drive, C drive, A (floppy) drive. You should also see an “X” drive, where you can store files for later use.

·         Your X drive is the most important drive of all, because you can access it from Bancroft as well as from the lab.

·         The rest of each PC's hard drive (the C and D drives) is "off-limits" to students in that you cannot write to them.


Your Unix Account

·         You also have an account on the CS Department's Unix machines.

·         Your instructor will give each of you a username and password in lab.

·         Although we may not make much use of it for this class, it is important that you log in now and change your Unix password to one you'll remember ... perhaps the same password as you just used for your PC account. To do this you:

1.      Launch the program PuTTy.

2.      Login to chessie.cs.usna.edu using the username and password provided by your instructor.

3.      At the command prompt type passwd and press enter.

4.      You will be prompted to enter your old password, and then your new password twice. Just follow the prompts, pressing return after each password you enter.

5.      Type logout to logoff of chessie.

Part 2: Your First Program

We will be using Microsoft Visual C++ 6.0 to create, compile and execute C++ programs. Microsoft Visual C++ 6.0 is an Integrated Development Environment (IDE) which contains:

·         an editor for typing in and modifying your program

·         a compiler for converting your C++ program into machine language

·         a linker which combines your object code with other sections of object code that might be required, resulting in an executable program.

·         a debugger for locating errors

Our C++ program (source code) will be entered as a file. This is the actual file that you will type using the editor. Other files will be used along with your source code to enable the IDE to execute the program. For example, another type of file that may make up your project is a "header" file, or "include" file. An "include" file allows you to use additional source code that you or others have written. They comprise a library of helpful tools -- for example, they can provide input/output capabilities, standard math functions (like cosine, or finding the square-root), among others. In fact, they are often called standard library files. You reference a "header" file by "including" it into your source file. You will include a library file today that provides basic input and output services for your program (details to follow).

The complete collection of files needed to run our program is termed a project. So, a project is the collection of files that make up the program (or application) that you are developing. The files for our program are grouped together in a project just as, in general, you would tend to group related files into a folder, and these related files are combined together by VC++ 6.0 to create a single executable program.

Step-by-step instructions to creating the "HELLO WORLD" program




Open up Visual C++ 6.0 (Start => Programs =>Microsoft Visual Studio 6.0 => Visual C++ 6.0



Select New




Projects and the Win32 Console Application template. Give a name to your project (HelloWorld for example). Recommend you click on Browse (the three dots), double click on the network drive labeled: "X:/". If there isn't already a folder within your X: drive, create a new folder and name it "IC210".  You should create a subfolder within the IC210 folder, labeled "Labs".  Click OK. The project name (HelloWorld) will be the name of the folder in your Labs folder. Click the OK button.




You have created a project named HelloWorld. The project contains no files. We will now add a file to the project that will contain the program source code. Select

Project => New…



Select C++ Source File, give the file a name (“main” in this example). Select OK


The next screen will display an empty Visual C++ window for the application development environment. Let’s take a moment to examine the parts of the IDE. The default workspace is comprised of three parts

The file view, which provides a "bird's eye" view of all the files that your programming project needs to run.

The editor window, in which you can type in your source code. The output window, which will display messages to you as the programmer whenever you compile or build or debug a project.

The editor uses something called syntax coloring to make it easier for you to read the programs you have written. Syntax coloring highlights the different program elements such as comments, keywords, numbers, and variables. This allows you to easily identify elements of your source code and find common syntax mistakes quickly. For example, the comments are in green. If you see your source code is green then you likely forgot to close a comment block. Additionally, the IDE attempts to help in other ways such as by providing automatic indenting, aligning braces, and so forth.


Create your main and add your include statements. If you do not understand what you are typing DO NOT BE CONCERNED. Your instructor will soon explain required and essential elements of C++ programs. Type the code exactly as shown below. When done, save your file: file => save (or select the save icon)



#include <iostream>
int main()
  std::cout << "Hello World!" << std::endl;
  return 0;


Compile. If you want the programs you write to actually do something then you must compile them (turn source code into machine language). Converting high-level C++ source code into machine language is very complex, but compiler vendors solve this problem for you. To compile your program, select Build => compile main.cpp (or click the compile button on the toolbar).


If your compile is successful you output window will give the following message:

--------------------Configuration: hello world - Win32 Debug--------------------
main.obj - 0 error(s), 0 warning(s)

If you do not see this message then get assistance from the instructor. If there are errors, they will be listed in the output window. Tips for deciphering error messages are given at the end of this tutorial.


Link and Run! Once you successfully compiled the program (no syntax errors) then you are to build the program by linking other included files to your program. Select Build =>Build main.exe (or click the build button). The “linker” will import required code and place it into your compiled program creating an executable file.

If successful, you will see the following message in the output window


--------------------Configuration: hello world - Win32 Debug--------------------
helloworld.exe - 0 error(s), 0 warning(s)


Once you have successfully compiled and linked your program, you are ready to execute it. Select the “execute” button and your program will automatically execute.


Program Output: You should see the following output window.

Now, minimize the VC++ 6.0 window. Double-click on My Computer and go to your lab folder. Inside you will see a folder called HelloWorld. Open it. There is a file named main.cpp – it is your source code file. Note the size of the file (~ 1 kb). Now, double-click on the Debug folder. The file titled HelloWorld.exe contains the executable file—that is, your program’s object code combined with all of the other necessary object code to run your program. Note the size (~220 kb)!! The increase in size is due to the code that was “included” or linked into your program.

Part 3: Let's do it all over again!

You just accomplished quite a bit; you typed in a C++ program, compiled it and executed it. It is important that you understand all the steps involved, and that you are comfortable using the Visual Studio 6 C++ IDE.

Repeat Part 2 of this lab all over again, but instead of entering the "Hello World" program, enter in this simple addition program.

// (Your name and alpha)
// This program adds two numbers
int main()
  int number1, number2, sum;
  number1 = 12;
  number2 = 13;
  sum = number1 + number2;
  std::cout << "The sum of these two integers is " << sum << std::endl;
  return 0;


Part 4: Let's make your program interactive

Modify your program so you can enter the two values to be added.  Replace

    number1 = 12;

    number2 = 13;


        std::cout << "Please enter two Integers: ";

    std::cin >> number1 >> number2;


Part 5: The "error" of your ways

When you make a certain kinds of common mistakes (syntax errors), the compiler won't be able to understand your program and will issue an error statement in the output window. You can double-click on an error in the Output window to go to the line containing that problem. Every syntax error must be corrected before a program will compile. Sometimes it is clear what's wrong, and other times you may have a hard time figuring out how to correct a syntax error. You will improve as you see enough errors to associate the messages with the syntax error. Here are a few simple rules and hints:  

·         Save program files before compiling

·         If an error pops up on a line that looks perfectly fine, sometimes it is the preceding line that is messed up. Check it too!

·         Check for missing or extra ; and } characters

·         Compile your program in small chunks. That way you can focus on errors in a small section of code. Waiting to compile until the entire program is completed can lead to large numbers of errors.

·         Messages that say "Cannot convert from ... to ..." usually mean you are trying to assign the wrong type to a variable.

·         Check your typing. A common mistake is to name a variable one thing but spell it wrong or use some other name later.

·         Sometimes, one simple problem can cause the compiler to find tons of errors (like forgetting to include "using namespace std;). Correcting that one line can make all of those errors go away. So, if you compile and see an unusually large number of errors don't panic. Often a simple change will correct them.

·         If you can't figure out what's wrong - ASK!!!


If time permits, take look at some error messages by making the following syntax errors in your addition program. Correct the error before introducing the next error into your program:

·         remove a semi-colon.

·         change a "<<" to a ">>".

·         change the variable number1 to Number1.

·         remove the ending }.

·         Remove the "#include <iostream>" line.

Finishing Up. You should now be comfortable with being able to generate source code, compile it, execute the program, and correct basic syntax errors. Let's clean up so you can get to your next class.

·         Click the Start button and

·         Choose the shutdown option...

·         ...and an option window ("dialog box") will appear. Your options are to

·         Log off...

·         Restart

·         Shutdown

·         Choose the "Log off..." option. CAUTION: Logging off will erase any work that you have saved to the C drive or the computer's desktop, so be sure your work-files are on your X drive BEFORE choosing this option!


Once you have your solution working: Show your working solution to your instructor.