Absolute Beginners C++

Lesson 2 - Data Types

2.1 Integer Variables

// ex2-1.cpp - integer variable
#include <iostream>
using namespace std;
int main()
    cout << "Enter a number : ";
    int value;
    cin >> value;
    cout << "You entered the number " << value << endl;
    cout << value << "*123=" << value*123 << endl;
Enter a number : 2
You entered the number 2
Three new concepts are introduced in example ex2-1.cpp. Firstly there is the concept of a variable - an object able to hold some value, and secondly there is the 'cin' object - a standard object that 'stands for' the console input stream (i.e. the keyboard), thirdly it demonstrates that arithmetic expressions are automatically evaluated wherever they occur.

The instruction 'int value;' tells the computer to reserve some memory for the storage of an integer value (a whole number), and to give that memory chunk the name 'value'. We could gave chosen alnost any other name for this variable: we are limited only that the name should begin with a letter, should contain only alphanumeric characters, and should not be a word used already to describe the language or a standard object. 

The instruction 'cin >> value' tells the computer to read in a sequence of keystrokes, to attempt to convert those keystrokes into a number, and to store the number in the reserved memory location called 'value'. The '>>' operator is an instruction to 'cin' to read something, and what it reads must of the type assigned to the variable 'value' which is an integer. 

The instruction 'cout << value' tells the computer to take the contents of the variable 'value', format it as a whole number and print it as a sequence of digits on the console output. When the computer gets to the expression 'value*123', it performs the necessary arithmetic and passes the result to cout so that it can be formatted and printed. 

2.2 Floating Point Variables

// ex2-2.cpp - floating-point variable
#include <iostream>
using namespace std;
int main()
    cout << "Enter a fractional number : ";
    double value;
    cin >> value;
    cout << "You entered the number " << value << endl;
    cout << value << "*1.23=" << value*1.23 << endl;
Enter a fractional number : 6.5
You entered the number 6.5
Example ex2-2.cpp introduces a second type of variable. The 'double' type of variable holds not whole numbers but fractional numbers. While an integer can hold values such as 4, 234, -56, or 0; a double can hold fractional values such as 1.6, -23.7, or 1.01E-5 (i.e. 1.01 x 10-5). 

You will see that in reading a double value, cin now allows a decimal point in the number, and in printing a double value, cout also prints a variable number of decimal digits depending on the value.

2.3 String variables

// ex2-3.cpp - String variable
#include <iostream>
#include <string>
using namespace std;
int main()
    cout << "Enter your name : ";
    string name;
    cin >> name;
    cout << "Hello " << name << "." << endl;
Enter your name : Mark
Hello Mark.
Example ex2-3.cpp introduces the third major type of variable: the 'string' type. String variables can hold variable length sequences of characters. In this example, cin is told to read a string and reads in a sequence of keystrokes until a space, tab or newline character is encountered. These keystrokes are stored in the string variable called 'name'. When cout is asked to print the variable name, it sends the characters directly to the console output with no additional formatting.

To use objects of type 'string' the compiler needs to be told what they are and what they do; this is done by the line '#include <string>'. This is an instruction to the compiler to find a definition of the standard string objects.

2.4 Exercises

a. Write a program (arith1.cpp) that reads in two integer numbers and prints their sum and product, as in: 
16 7
Sum is 13
Product is 42
b. Tidy up arith1.cpp (arith2.cpp) with a prompt and clearer output, as in: 
Enter two integer numbers: 6 7
6 + 7 = 13
6 x 7 = 42
c. Write a program (average4.cpp) that takes in four integer numbers and prints their average, as in: 
Enter number 1: 6
Enter number 2: 3
Enter number 3: 5
Enter number 4: 11
Average is 6.25
d. Write a program (loves.cpp) that asks for your name and the name of something you love, and then says "<name> loves <thing>.", as in: 
Enter your name : Mark
Enter something you love : ice-cream
Mark loves ice-cream.

© 1999 Mark Huckvale University College London