This chapter is going to discuss constants, variables, arithmetic operators and input.

A constant is a number that cannot change it's value through the life of the program.  A constant can be created several ways in C++.  A number written in the source code is a constant.  A constant can be declared so that no other value would ever be able to go into it by using the attribute const.  Another way to create a constant is to use the preprocessor with #define.

Most C++ programmers use upper case letters and underscores "_" to name constants.  I will try to stick to that convention throughout this tutorial.  The following are valid ways to create constants:
#define MAX_SIZE 90
const int MAX_SIZE = 90;

The preprocessor is a part of the compiler that searches for code that should be defined before going logically through the program.  The average compiler usually goes through code at least three times.  The first time is to look for preprocessor statements.  You have already seen two uses of the preprocessor.  The first use was the #include <library.h> and now #define THE_ANSWER 42.

Valid names for constants, functions, classes, and variables are flexible in C++.  The names can begin with a letter or underscore "_".  Names may have digits in them and may end with digits but cannot begin with digits.  Names can be either case or a mixture of upper and lower case letters.  Names cannot conflict with keywords.  Names can be as long as 247 characters!  That means we could use the following as a valid name:


The above name could actually be longer but the editor I use crashes whenever I make it larger than 183 characters!  Please don't ever use a name that long because it is a pain to read and to write!

Now it is time to discuss comments.  Comments are words or code that are ignored by the compiler.  In C++ there are two ways to create comments.  The first way is to use // before your comment.  Everything on that line after the // will be ignored by the compiler.  Another type of comment, that can span multiple lines, starts with /* and ends with */ .  Here is an example of both comments:

// I use this type of comment more than the other type. Usually when using 
// the other type people have to surround the edges to keep from confusion.

/* If code was commented out for what ever reason it could be hard to
see where the comment starts and ends unless the programmer has a color
coded compiler.  A color coded compiler like Visual C++ and Borland
C++ usually gives a special color to comments.  Of course that doesn't
help much if your color blind! */

* To make comments readable in text editors programmers often place asterisks *
* around their comments.  This also makes them look pretty!                   *
Comments are very important for professional programmers.  Student programmers are forced to use comments or their grades will pay for it.

Many pieces of software are indecipherable due to a lack of comments.  This has occasionally created job security!  At MEDITECH the development programmers seem to be allergic to comments.  I write custom code and fix bugs in the software which is often complicated with uncommented programs.  If you ever program with other people PLEASE use comments.  comments help your partner(s) work with you.  Generally I identify all my variables as well as code that seems complicated to me.

There are several data types in C++.  My favorite data type is int, or integer.  int types can not have decimal points and for most compilers can range from –32,768 to 32,767.  Here are more data types:
Type Name
Other Name
Range of Values
int system dependent * signed * integer system dependent 
unsigned int system dependent * unsigned integer system dependent
__int8 1 signed character -128 to 127
__int16 2 signed short integer -32,768 to 32,767
__int32 4 signed integer -2,147,483,648 to 2,147,483,647
__int64 8 large integer -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807
char 1 signed character -128 to 127
unsigned char 1 unsigned character 0 to 255
short 2 signed short integer -32,768 to 32,767
unsigned short 2 unsigned short integer 0 to 65,535
long 4 signed long integer -2,147,483,648 to 2,147,483,647
unsigned long 4 unsigned long int 0 to 4,294,967,295
enum same as int none same as int
float 4 none 3.4E +/- 38 (7 digits)
double 8 none 1.7E +/- 308 (15 digits)
long double 10 none 1.2E +/- 4932 (19 digits)
* bool 1 bit boolean 0 to 1
* signed means the number can be positive or negative.

* an integer is a number without decimal values.

* unsigned means the number must be positive.

* A boolean value can be either 1 or 0.  bool is the smallest data type, representing one bit in memory.  bool is not implemented in most compilers.  Visual C++ 5 and Borland C++ 4 were the first to use bool.  Since it is not a part of Visual C++ 4 I will unhappily go without using bool in this part of the tutorial.  The reason I like bool is for True/False values.

We need a few operators before we get to the next program.  We should start with the assignment operator = .  The name that will get assigned a new value must be on the left of the operator.  When I started programming I missed that piece of information.  It took me a week to figure out what I was doing wrong!  Some other useful operators are + for addition, - for subtraction, * for multiplication, / for division and % for modulus.  If you don't know what a modulus is I'll discuss it later.  Parentheses are used to alter the order of operations.

int Variable;  //Hey look it's a comment! Variable will hold a value

Variable = 32 + 5 - (9 / (2 + 7)) % 1;  // Variable will get the number 37

To avoid using keywords I always start a variable's name with a capital letter.  I will use this convention throughout this tutorial.

cin - cin is the opposite of cout.  It takes keyboard input.  cin takes the right bit shift operator to place characters ending with a carriage return.

Now here is program2:

< Code > < Program >

// main2a.cpp - This program is an adding machine for the C++
// game tutorial.

#include <iostream.h> // cin and cout are defined in this header

void main()
 double Input=0; // This will hold keyboard input
 double Total=0;  // This will hold the total

 // Type a welcome message
 cout<<"Welcome to the C++ Game Tutorials Adding Machine"

 //Loop till the user enters -666
  cout<<"Enter a value (-666 to exit): "; // Prompt user
  cin>>Input;        // Take input 
  Total = Total + Input;     // Add total
  cout<<endl        // Print total
      <<"Total = "<<Total<<endl;
 cout<<"Good Bye!"<<endl<<endl;

OK, so I skipped two parts of the above program.  Don't worry it's not as overwhelming as it may seen.  Let's do the easy part first.  The not equal to operator, != .  With this we could say 5 != 4 and get a true value because 4 is not equal to five in this universe . . ..

The while loop.  The while loops syntax is:
    code that will be executed till the condition is false

In Program 2a we looped till the user input = -666.  If the input wasn't = -666 the condition would be true.  The code in the braces is executed from top to bottom and then the condition is reevaluated.  The loop will only be exited if the code reaches the bottom of the loop and the condition is false.  BE CAREFUL BECAUSE YOU COULD CAUSE AN INFINITE LOOP.  An infinite loop is the bane of all programmers.  An infinite loop happens when the condition is always true.  If you ran the following code you would have to use the operating system or the power button:

while( 42 != 4 )
   cout<<"Stop me if you can!"<<endl;

In the next chapter we will work on a roulette game, yes I said game!  The concepts will mostly be an intensification of what I have already introduced.  As I've said before I REALLY want feedback about the tutorial.  If you have comments or ideas please send them to