Lab 2: Types


In this lab, you will perform several experiments with different types of data using a pre-written program.


Starting with this lab exercise, each lab exercise will have a section named "Files", and it will contain information like this:

Directory: lab2

You should create the specified directory and download the files listed here. Every lab will have just the one directory, but any labs will have multiple files which should all be downloaded and saved in the directory that you create.

You will also be asked to personalize the documentation at the beginning of the files. For this lab, add some lines to the comment at the top of the file similar to this:

* Modification history:
*    by John VanDoe in September 2002 for CPSC 185 at Calvin College
*       Modified to run the experiments for Lab #2.
You should make similar additions to every code and documentation file you see during any lab.

C++ Output

All of the things we look at in this lab are really internal to the workings of a program. In order to get some feedback, the program needs to display, or output, some information.

Output in C++ is done with the cout object. This is the name of the screen in C++. To actually send something to the screen, C++ gives us the << operator. In general, an output statement looks like this:

cout << Value1 <<  Value2 << ... <<  ValueN;

where each ValueI is replaced with objects. The << operators separate each of the values. For now, you can send any object to the screen this way.

Compiler Errors

Most (if not all) of the experiments for this lab ask you to compile program that will deliberately generate compiler errors. This is to get you familiar with the error messages from your compiler.

Helpful hint: The first error message from a compiler is the only one that matters.

This is a bit overstated, but not by much. Usually one mistake can generate several error messages. The compiler may generate several messages for the same mistake; it can also get so confused by the problem that it starts to complain about good code!

Consequently, it is fastest to fix the first error message you get and then recompile. If you try to fix all of the error messages, you'll waste too much time trying to figure out if your fix for the first error already fixed the other errors. When you recompile, you'll often get a whole new batch of errors, but as long as you keep working on the first error message each time, you should eventually arrive at a program that compiles.

In these experiments, you'll be asked to write down the first error message when you're deliberately told to compile a program with an error in it. Write down the error message verbatim as you read it off the screen. Write down only the first error message since it's really the only one that matters.

Most error messages are quite awkward, and it'll help you to recognize what they mean if you write them down carefully. Try to pick out key words (e.g., "initialization", "undeclared", etc.) in the error message that indicate what's going wrong. Learn to recognize these words.

Compilers will also issue compiler warnings for code that's officially correct according to the C++ standard, but probably it causes some problem. If a program generates only warnings from the compiler, but no errors, you often get a executable program (depending on the compiler), but most likely there's a bug in the program that will be much harder to find at run time. Heed your compiler's warnings!

The Experiments

This lab consists of several experiments. Your instructor may ask you to do only some of these, be sure to do those first; you will find working on the others helpful, if you have the time.

Experiment #1: Declaration experiments

Experiment #2: Integer experiments

Experiment #3: Real-number experiments

Experiment #4: Character experiments

Experiment #5: String experiments

Experiment #6: Constant experiments


Submit your answer to the questions for the experiments that you completed. Your instructor may also ask you to submit a copy or multiple copies of your program.


compiler error, compiler warning, display, output
Lab Home Page | Prelab Questions | Homework Projects
© 2003 by Prentice Hall. All rights reserved.
Report all errors to Jeremy D. Frens.