Source Code. Projects. Nerd Stuff. Art Stuff.

Lesson 2: Data Types, Output, and Calculations

Data Types, Output, and Calculations

Console Output

  • cout – used to send output to the screen (using the text console window). The insertion operator (<<) is used to separate each piece of output. Literal numbers, strings, and characters may be output, and variables and expressions may have their values output as well. Remember that string literals must be enclosed with quotation marks (“).
  • Escape Sequences: special character combinations to represent special characters:
    • 1


    • 1


    • 1

      one backslash

    • 1

      quote character


        cout << "Here is the first line of";
        cout << " text.\nHere is the second line.";
        // notice we can write a line of text over several
        // lines of code as long as we break outside a set of quotes.
        // This keeps your program listing from running off the
        // right edge of the page.

Identifiers (Programmer-Defined Symbols)

  • Identifier – the name of a variable or function, which is made up by the programmer, and should give the reader some hint of what it is used for. (e.g. number_of_candy_bars is a very good, clear variable name.) C++ is case-sensitive with all identifiers and keywords.
  • Indentifier Rules – Identifiers must follow the following rules: They must start with a letter or underscore, and contain only letters, digits, or underscore. E.g. variables called 3X or %change are not allowed.
  • Reserved Words – pre-defined names (keywords) in C++ must not be used for your own identifier names (for example you can’t name a variable “int”.)


  • Variable – a named location in memory that can be used to store a specified type of data, and whose value may be changed in the course of the program. Variables must be declared before they are used in C++.
  • int – a datatype that stores an integer (no decimal value allowed). Can be positive or negative.
  • Variable declaration examples:
       int x;
    int age, height;
  • Uninitialized variables – when a varaible is first declared but has not yet been given a value, we say that it is uninitialized. That means its value is unknown, and could be anything. (Probably dependent upon whatever else was stored in that memory location earlier.) Make sure you initialize a variable before you use its value.

firstVars.cpp: Here is an example demonstrating the use of variables in output.

In-class exercise: Write a program to output today’s date, using at least two integer variables.

date.cpp: Solution to in-class exercise to output today’s date, using at least two integer variables.

Data Types

  • Floating Point – a term referring to real number approximations stored in computers
  • double – floating point number, uses 8 bytes on most modern systems
  • float – floating point number, uses 4 bytes on most modern systems
  • long double – floating point number, uses 10 bytes on most modern systems
  • int – integer number, uses 4 bytes on most modern systems
  • short – integer number, uses 2 bytes on most modern systems
  • long – integer number, uses 4 bytes on most modern systems
  • char – stores one character (one keyboard key). Character literals are enclosed with apostrophes (single quotes), e.g. ‘w’ uses 1 byte on most modern systems
  • bool – stores a Boolean value. Literal values are: true or false with no quotes.
  • To determine the size of a data type or variable on your particular system, use the sizeof operator (which acts like a function)

Calculations, Assignments, Formatting Text

  • Arithmetic operators:

    for multiplication


    for addition


    for subtraction


    for division (beware of integer division, as explained below)


    for modulus (the remainder from integer division)


    for assignment (stroring the result of operation at right in variable at left; think of it like an arrow to the left)

  • Integer division produces an integer result. To get a floating point result, one or both operands must be a floating point value.

Programming Style

  • Programs are written to be read by other humans as well as the computer.
  • Use my sample programs and those in the book as your guide for style.
  • Indentation: always indent consistently. It greatly improves readability
  • Indentation reflects the level, or scope, of a line of code.
  • The contents of a set of curly braces “{” and “}” are always indented
  • You should always have Comments at the top of your program, stating your name, the name of your program source file, the name of the class and assignment, and a brief description of what the program does.
  • Comments should also be used to explain hard-to-understand code, and to summarize big steps such as functions and sometimes loops.
  • Use helpful variable names (strive for self-commenting code – clear without comments to explain it.)
  • Provide a good user interface, including prompting for inputs and explaining outputs.

calcDemo.cpp: Here is an example demonstrating how variables and data types work in calculations.

In-class exercise: Write a program using at least 3 variables to calculate and output the average of the following three test grades: 90, 81.2, and 88.77

Solution to above exercise: gradeAverage.cpp