Prog 1 Module Lesson 7
Prog 1 Module Lesson 7
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
Course Materials:
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.
or
43
cout <<
The extraction operator (<<) can be used more than once with a combination of
variables, strings and manipulators (like endl or \n).
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.
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;
}
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.
To make your output more readable, insert at least one space before displaying the
value of your variable.
45
cout <<
Example 3
#include <iostream>
using namespace std;
int main ()
{
int year = 2020;
cout << "Hello BSIT Batch " << year;
return 0;
}
Example 4
#include <iostream>
using namespace std;
int main ()
{
int currentYear = 2020;
int birthYear = 1989;
return 0;
}
46
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:
and
Hello
World!
if you want to put a blank space in between lines your statement must be like this
or
47
cout <<
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
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
The following escape sequences can be used along with the cout command
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;
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 <<
Infected = 25432
Recovered = 4235
Died = 1453
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