Lab 2: Types


In this lab, you will perform several experiments with different types of data.


Most of the lab exercises from here on will have a section named "Files", and it will contain information files and/or links to files that you will use.

/* experiment.cpp is a program to run data type experiments.


#include <iostream>
using namespace std;

int main ()

  int count = 3, 
      number = 5;

  cout << "count is " << count << "\n"
       << "number is " << number << endl;

You should download/copy this file into whatever directory/folder/project you are using for Lab 2. Each lab exercise will usually have one or more files for you to use.

Also, as the opening documentation indicates, you are to personalize this documentation. for example,

   Modified by John Doe for CPSC 104X on December 31, 2012
   to run the experiments for Lab #2.

C++ Output

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 << item1 <<  item2 << ... <<  itemn;

where each itemi is some object — a label, a variable, a constant, an expression. The << operators separate each of these items. For now, you can send any object to the screen this way.

Compiler Errors

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

Helpful hint: The first error message from a compiler is (usually) the most important one.

The reason for the preceding statement is that one mistake can generate several error messages. The compiler may generate several messages for the same mistake; it can also get so confused by an error that it starts to complain about good code!

Consequently, it is wise to fix the first error message you get (or perhaps the first few if you spot something obvious that's wrong) 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 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 that appears when you compile a program with an error in it. Write down the error message verbatim (or copy and paste it) as you read it off the screen. Write down only the first error message.

Error messages may be quite obscure and it will 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 it probably 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 or parts of them; be sure to do those first. You will probably find it helpful to work on the others also, 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

Lab Home Page |  Homework Projects

Report errors to Larry Nyhoff (