Topic 3: Input & Output using standard device

cout statement

The cout (pronounced as see out) object is an output object that sends data to a standard output display device. It is used with the insertion operator (<<) to display information on the screen. Information that is sent to the screen can be any combination of literal constants, named constants, and variables.

Following is a syntax for writing a cout statement, note that the part in square brackets is optional which can be used to print multiple items in the same statement:

Syntax:
  1. cout << item1 [<< item2 << itemN];
Figure 3.1 uses cout statement to display Hello There world!
...
Figure 3.1

A stream manipulator is used to manipulate (manage) the characters in an input or output string. endl is a stream manipulator that moves the cursor to the next line on the screen. It's the same as pressing the Enter key on the keyboard, as shown in Figure 3.2. Figure 3.3 shows combining cout statements (the cout from line 7 and the semicolon at the end of line 6 from Figure 3.2 is removed). Note that both with have the same output.

...
Figure 3.2
...
Figure 3.3
Output:
  • Hello World!
    This is my first program.

Escape sequence

The escape sequences are special non-printing characters that are used to control the printing behaviour of the output stream objects (such as ‘cout’). The escape sequences and the characters they represent are given in Table 3.1:

Table 3.1
Escape sequence Character represented
\a Alert (bell, alarm)
\b Backspace
\f Form feed (new page)
\n New-line
\r Carriage return
\t Horizontal tab
\v Vertical tab
\’ Single quotation mark
\” Double quotation mark
\? Question mark
\\ Backslash

... Output: ...
Figure 3.4
... Output: ...
Figure 3.5

Formatting Numeric Output

Real numbers are displayed in either fixed-point or scientific (e) notation:
  • Small real numbers (six or less digits before decimal place) displayed in fixed-point notation
    1. Example: 1,234.56 displayed as 1234.560000
  • Large real numbers (more than six digits before decimal place) displayed in e notation
    1. Example: 1,225,000.00 displayed as 1.225e+006

Stream manipulators allow control over formatting:
  • fixed stream manipulator displays real numbers in fixed-point notation
  • scientific stream manipulator displays real numbers in e notation
  • Stream manipulator must appear in a cout statement before numbers you want formatted

Table 3.2
Example Result (Display)
double total = 20535.75;
cout << fixed;
cout << total << endl;
20535.750000
double conversion = 4.9018436;
cout << fixed << conversion << endl;
4.901844
double total = 20535.75;
cout << scientific << total << endl;
2.057575e+04

Getting data from keyboard

C++ uses stream (sequence of characters) objects to perform input/output operations. The cin object is used to get data from the keyboard. The program pauses while the user enters data. The extraction operator (>>) takes information out of cin object and stores it in internal memory

Syntax:
  1. cin >> variableName;

A prompt (message) must be used to let the user know what data is to be entered.

Table 3.3
Example 1 Example 2 Example 3
int num;
cout << “Please enter a number: ”;
cin >> num;
double height;
cout << “Please enter height: ”;
cin >> height;
char response;
cout << “Please enter y or n: ”;
cin >> response;