Introducing the Output Stream 'cout'
Learn to write a program that displays the table of a number using the 'cout' statement in C++.
In this lesson, we will write a program in C++ that displays a table of a number.
We’ll begin with basic C++ program elements, including the cout
instruction, the syntax error, and some basic arithmetic operations. Finally, we’ll write a simple table printing program using the cout
instruction.
Basics of writing a C++ program
Let’s begin by learning to display a simple message on the console in C++.
Displaying a message
Here is our first C++ program, which prints the message Hello Turing! Welcome to the world of C++
.
#include <iostream>using namespace std;int main(){// your code goes herecout << "Hello Turing! Welcome to the world of C++" << endl;}
-
Line 1: The library
iostream
contains definitions of the input and output functionality that we can use in our program. The#include
keyword is the syntax to include any which we would like to add in our program.library a collection of pre-written codes that can be reused by programmers in their programs Note that in C++,
#include
is a preprocessor directive that is used to include both header files and libraries in C++ programs.
-
Line 2:
using namespace std
allows us to use the members (cin
,cout
,endl
, etc.) of thestd
in the current scope without having to specify the namespace for each member. This can help us avoid naming conflicts with other variables in our program.namespace a declarative region for grouping identifiers (e.g. class names, functions, variables) to avoid naming conflicts and organize code -
Line 3: This is left empty to improve code readability.
-
Line 4: This is where our program starts. Every C++ program must have only one
main()
. -
Lines 5–8: The curly brackets
{ }
represent the body of ourmain()
.function a block of code that performs a specific task -
Line 6: This is a comment. A compiler (software that converts our program of C++ to machine code, which is the only code our hardware machine understands) does not execute a comment. The comments are just added to make the code more readable.
-
//
is used to comment a single line.// This is a single-line comment
-
For multi-line comments, we use
/*
and*/
, and write the body of the multi-line comment inside./* Line 1 of the comment Line 2 of the comment Line 3 of the comment */
-
-
Line 7:
cout
is the standard instruction to display messages on the console. The symbol<<
represents the C++ output operator, andendl
is used if you would like to move the cursor of the console to the new line. Every message in double quotes ("
) is passed to the output pipe, and that message is passed to the screen and displayed there.Note: All statements should have a semicolon (
;
) at the end.See the animation below.
Header files contain predefined functionalities that can be used in our programs. For example here
cout
statement is a functionality to print on the console screen, and by includingiostream
(the library) we can use that functionality in our program. If we do not includeiostream
, our program will not recognize thecout
instruction.
Displaying multiple messages
We can print multiple messages using a single cout
like this:
cout << "message 1 " << "message 2 " << "message 3 ";
or use multiple cout
statements to print the same message like this:
cout << "message 1 ";
cout << "message 2 ";
cout << "message 3 ";
Exercise
Write the code in the code editor below to display the three words "Hello "
, "Turing "
, and “again!
”
a. Use 3 separate cout
instructions
b. Use one cout
and three messages.
Make sure to move the cursor to the next line after printing “Hello Turing again!” on the console.
We’ve already included the header files,
namespace
, and themain()
function. You just need to add thecout
instructions.
{// Write your code}
Since we know the basics of a C++ program, it’s important that we also talk about syntax errors, as these errors can be very frustrating when writing code, and learners should be careful about them.
Syntax errors in the program
C++ has many rules for writing code. A syntax error is a violation of any rule of the language. The compiler cannot execute a program if there’s an error in the syntax. Now let’s see an example of a basic syntax error.
There is a syntax error in the following code. Can you guess what it is without running it?
Press “Run” and see what the compiler suggests to fix the error.
#include <iostream>using namespace std;//error in line number 7int main(){cout << "Hello" << endlreturn 0;}
Fix the error and rerun the program. If it does not work, look at the solution below.
The compiler will mostly catch all the syntax errors, but sometimes it might not. It would usually suggest the point where something could be wrong in the code, but the suggested location/reason may not always be the exact location/reason.
To print a table, we only need the multiplication instruction, but let’s also look at the syntax of some other basic arithmetic operations (+
, -
, *
, /
) that we can perform in a C++ program.
Basic arithmetic operations (+
, -
, *
, /
)
The program below shows all simple arithmetic operations:
#include <iostream>using namespace std;//This program performs different arithmetic operationsint main(){ //endl inserts new linecout << "2*3="<<2*3<<endl<<"2+3="<<2+3<<endl<<"2-3="<<2-3<<endl<<"4/2="<< 4/2 <<endl;return 0;}
Run the above code and see the output.
In line 8, cout
is working in the following way:
-
Everything written in between the two
<<
symbols ill be printed one by one, from left to right. -
If the message inside the two
<<
symbols is within double quotes ("
), then it will be printed as it is. -
If we write a mathematical expression without quotes, C++ is intelligent enough to replace mathematical expression with its result.
The mathematical expression will be first evaluated (automatically in the background), and that evaluated value will be replaced as the message, e.g.,
2*3
will be replaced by6
. Similarly,2+3
will be replaced by5
,2-3
will be replaced by-1
, and4/2
will be replaced by2
. Therefore, during execution, the interpretation of the line 8 will automatically become:
cout << "2*3="<< 6 << endl << "2+3=" << 5 << endl << "2-3="<< -1 << endl << 2 << endl;
We are ready now for our first attempt at the table-printing program.
Printing a table
Now that we know the basics, we can start with our table printing program using what we’ve learned above.
As an example, let’s take , for which we would like to print the table like this:
23 x 1 = 23
23 x 2 = 46
23 x 3 = 69
23 x 4 = 92
23 x 5 = 115
23 x 6 = 138
23 x 7 = 161
23 x 8 = 184
23 x 9 = 207
23 x 10 = 230
The idea is to use cout
and C++'s ability to solve mathematical expressions to print the table line by line.
The program below prints the table of 23:
#include <iostream>using namespace std;//This program should print the table of 23 up to 10th multipleint main(){cout << "23 x 1 = " << 23*1 << endl;cout << "23 x 2 = " << 23*2 << endl;cout << "23 x 3 = " << 23*3 << endl;cout << "23 x 4 = " << 23*4 << endl;cout << "23 x 5 = " << 23*5 << endl;cout << "23 x 6 = " << 23*6 << endl;cout << "23 x 7 = " << 23*7 << endl;cout << "23 x 8 = " << 23*8 << endl;cout << "23 x 9 = " << 23*9 << endl;cout << "23 x 10 = " << 23*10 <<endl;return 0;}
In this program, cout
is used to print the table of 23 up to its 10th multiple.
Exercise
-
Now, write a program in the code editor below to print the table of 35.
-
After writing the table of 35, modify your program in the code editor below to print the table of 43.
#include <iostream>using namespace std;int main(){// Add code herereturn 0;}
Now, if you completed the exercise above, you would know how inconvenient and monotonous it is to change the code again and again whenever we wish to print some other number’s table.
Wouldn’t it be super convenient if we had a way to get the same results but with the minimum required changes in the code?
Luckily, there is! We can use variables instead. Let’s learn how in the next lesson.