cout <<
UNIT 7: OUTPUT STATEMENT
Introduction
All computer programs created involve displaying something on your screen or monitor.
It can be the result of a computation, a prompt asking the user to enter something from the
keyboard or it can be a reminder or an instruction that the user must follow while using the
program.
When displaying something on your screen, make sure that it is readable, meaning there
are ample spacing between words and between lines. The instructions and prompt must be
clear and concise. Use words that are familiar to the user of your program.
Learning Objectives
After successful completion of this lesson, you should be able to:
a. Use cout in your program to display a message, an output or a prompt.
b. Differentiate the use of endl and \n.
c. Use other escape sequences together with cout
Course Materials:
7.1 cout statement
The cout command in C++ is use to display the output to a standard output device like
your monitor or screen. It is defined in the <iostream> header file. So, if you do not include
the lines: #include <iostream> and using namespace std; at the beginning of your
program, errors will appear when you compile your program and your program will not run.
The “c” in cout refers to “character” and “out” means “output”. The cout object is used
along with the insertion operator (<<) in order to display a stream of characters.
The general syntax is:
cout << varName;
or
cout << “some string”;
43
cout <<
The extraction operator (<<) can be used more than once with a combination of
variables, strings and manipulators (like endl or \n).
cout << “some string “ << varName << endl;
Note: After cout there should be an insertion operator (<<) and at the end of your statement
there must be a semicolon (;). You can put a space before and after the << to make your
statement more readable. Spaces between double quotation marks (“ “) are counted and
displayed. Press < (less than symbols) twice to make << (no space in between).
If you typed 10 spaces in between the words you want to display then you can see 10
spaces in your screen. The compiler will not truncate the excess spaces.
cout “Hello World!”;
will display:
Hello World!
Example 1
In this example, we will display a message on the first available line on your screen. The
message must be inside a double quotation mark (“ “).
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello BSIT Batch 2020";
return 0;
}
The output of the program:
44
cout <<
Example 2
In this example, we will display the value of a variable. A variable can hold a numeric
value, a character or a string.
#include <iostream>
using namespace std;
int main ()
{
int year = 2020;
cout << year;
return 0;
}
Note: It is not a good programming habit to just display a value without describing
what it is to the user.
So instead of just using
cout << year;
you can write,
cout << “Current Year: “ << year;
To make your output more readable, insert at least one space before displaying the
value of your variable.
If you write: cout << “Current Year: “ << year;
The output will be: Current Year: 2020
If you write: cout << “Current Year:“ << year;
The output will be: Current Year:2020
45
cout <<
Example 3
In this example we combine a string and a variable in one cout statement.
#include <iostream>
using namespace std;
int main ()
{
int year = 2020;
cout << "Hello BSIT Batch " << year;
return 0;
}
Example 4
In this example we will use cout to display result of a direct computation.
#include <iostream>
using namespace std;
int main ()
{
int currentYear = 2020;
int birthYear = 1989;
cout << "Age: " << currentYear - birthYear;
return 0;
}
46
cout <<
7.2 Using endl and \n together with cout
Both endl and \n serve the same purpose in C++ – they insert a new line before they
display the next item or message. But an
endl must appear together with << and is outside the “ “ while \n must be inside
the “ “.
Example:
cout << “Hello” << endl << “World!”;
and
cout << ”Hello\nWorld!”
will produce the same output
Hello
World!
if you want to put a blank space in between lines your statement must be like this
cout << “Hello” << endl << endl <<“World!”;
or
47
cout <<
cout << ”Hello\n\nWorld!”
Hello
World!
Using endl and \n is like pressing the Enter key on your keyboard. To have multiple
blank lines in between your output you have to write down more than one endl or \n in your
cout statement Let’s say you want three blank lines between Hello World!, your cout <<
would be like this:
cout << “Hello” << endl << endl << endl << endl <<“World!”;
or
cout << ”Hello\n\n\n\nWorld!
Hello
World!
Note: How many endl or \n will you type if your want multiple blank lines between?
Answer: desired number of blank lines +1
7.3 Escape Sequences
The following escape sequences can be used along with the cout command
\' single quote
\" double quote
\? question mark
\\ backslash
\a audible bell
48
cout <<
\b backspace
\f form feed - new page
\n line feed - new line
\r carriage return
\t horizontal tab
\v vertical tab
Example 5
This program will show the effect of some of the above escapes sequences.
#include <iostream>
using namespace std;
int main ()
{
cout << "With endl--" << endl;
cout << "BSIT Batch 2020" << endl;
cout << endl << "With \\n" << endl;
cout << "BSIT Batch 2020 \n";
cout << endl << "With \\t" << endl;
cout << "BSIT \t Batch \t 2020" << endl;
cout << endl << "With \\b" << endl;
cout << "BSIT\bBatch\b2020" << endl;
cout << "BSIT \bBatch \b2020" << endl;
cout << "BSIT\b Batch\b 2020" << endl;
cout << endl << "With \\ \" " << endl;
cout << "\"BSIT Batch 2020\"" << endl;
cout << endl << "With \\' " << endl;
cout << "\'BSIT Batch 2020\' " << endl;
cout << endl << "With \\" << endl;
cout << "BSIT\\ Batch\\ 2020" << endl;
cout << endl << "With bell \\ a" << endl;
cout << "BSIT\a Batch\a 2020\a" << endl;
return 0;
}
49
cout <<
Activities
1. Edit the program in Example 3, the program should display 5 blank lines before
Batch 2020.
Hello BSIT
Batch 2020
2. Write a program that will display your first name, middle name and last name in
separate lines with blank space in between. Use only one cout statement.
Example output:
Roi Eldrick
De Jesus
Villanueva
50
cout <<
3. Write a program that declares the following integer values:
Infected = 25432
Recovered = 4235
Died = 1453
Then display the number of active cases.
Example screen output:
Corona Virus Report:
Infected = 25423
Recovered =4235
Died = 1453
Active Cases = ?
Online References
https://www.geeksforgeeks.org/escape-sequences-c/
http://www.cplusplus.com/doc/tutorial/basic_io/
https://www.programiz.com/cpp-programming/library-function/iostream/cout
51