IC210

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 COB the next day or will receive a grade of 0. See the course policy for definitions on “COB.”

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.

·         You will start using this in the spring. 

Part 2: Your First Program

We will be using Microsoft Visual C++ 2005 Express to create, compile and execute C++ programs. Microsoft Visual C++ 2005 Express 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++ to create a single executable program.

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

STEP


INFORMATION


1

Open up Visual C++ 2005 Express (Start => Programs =>Microsoft Visual C++ 2005 Express Edition =>  

 

2

Select New

 

3

 

Projects and the Win32 Console Application template. Look at the picture below.  First, make the top part look the same on your screen (make sure “Win32 Console Application” is selected). Then, give a name to your project (HelloWorld for example. Unlike the picture, do not include spaces in any of your project or file names). Recommend you click on Browse, 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.

 

 

4

Select “Next”

 

 

5

Select the “Empty Project” check box under “Additional Options” and then click on “Finished”

 

6

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.  Right click on the file “Source Files” select “Add” and then “New Item” 

 

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

5

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.

6

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)

 

 

7

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 (or click the compile button on the toolbar).  

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

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.

8

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 Solution (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

 1>------ Build started: Project: Hello World, Configuration: Debug Win32 ------

1>Linking...

1>Embedding manifest...

1>Build log was saved at "file://x:\IC210\Hello World\Hello World\Debug\BuildLog.htm"

1>Hello World - 0 error(s), 0 warning(s)

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

              

9

Once you have successfully compiled and linked your program, you are ready to execute it. Select Debug => “Start Without Debugging” your program will automatically execute.

10

Program Output: You should see the following output window.

Now, minimize the Visual C++ window. Double-click on My Computer and go to your lab folder. Inside you will see a folder called HelloWorld. Open it. Inside you will see a second folder 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 move up one directory and 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 (~44 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 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
 
#include <iostream>
 
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;

with

        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.

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

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!