The Output Stream: cout Statement

Topic 8 of 13
  1. Dear developers, welcome to your C++ learning journey! This comprehensive guide provides detailed notes, designed to help you master the fundamental mechanics of writing C++ code. Whether you’re an absolute beginner or looking to solidify your basic knowledge, understanding how instructions are processed and displayed is step one in learning C++ step-by-step.
  2. Previously, we discused the statements in the C++ and its type. In this post, we will learn how to display outputs to the screen at the time of execution. You guessed right: the cout statement.
  3. The cout Statement: Giving Output

The cout statement is one of the most common ways to display data to the user on the screen.

Defining cout Statement:

The term cout stands for Character Output. It is the object of the ostream (output stream) class, used to show output to the standars output device (monitor).

  • Stream Object Concept: To understand cout, consider a real-life example: Imagine a river is the entire source of water, which we call the stream. If you need water (data) to reach your field (the computer screen), you must create a small canal or stream to channel that water.
  • The Analogy: In C++, the main resource included at the top of your program is the iostream (Input/Output Stream). The cout object acts as the channel that takes data and outputs it to the screen.
  • Functionality: cout works by processing a stream (a sequence) of characters or data and delivering that sequence to the screen.

Anatomy of a cout Statement

A typical output statement looks like this: cout << Expression;

Part Description (C++ Terminology) Function
cout Stream Object Channels data out to the screen.
<< Stream Insertion Operator (or simply Insertion Operator) Consists of two smaller-than signs. It takes the data (expression) on its right and sends it into the cout stream for printing. It is a mandatory part of the cout syntax.
"Expression" Expression A piece or part of the code that is evaluated to produce output.
; Statement Terminator Indicates the end of the statement.

Understanding Expressions and Double Quotes:

The expression is the content you want to display, such as "Seven Subscribes".

  • When the compiler sees an expression enclosed in double quotes(""), it understands that no complex mathematical or logical operation needs to be performed.
  • The only operation is to print the text inside the quotes exactly as it isonto the screen.

Try It Yourself Challenge #1: Deconstructing the Statement > > In your own words, explain the relationship between the iostream, the cout object, and the Stream Insertion Operator (<<). How do these three components work together to display the word "Hello" on the screen? Comment your answer.

  1. Handling Output Formatting: The Line Break Problem

When you write multiple cout statements, even on separate lines in your code, they will all run together on a single line of output.

Example Behavior:

# include <iostream>
using namespace std;
int main{
    cout<<"Hello!";
    cout<<"Codders";    
    cout<<"From 7Scribes";
    reutrn 0;
}

Output: Hello!CoddersFrom 7Scribes

To correct this behavior and force subsequent output onto a new line, C++ uses special features called Manipulators and Escape Sequences.

  1. Manipulators:

Manipulator is a built-in function used within the code to change the behavior of the output stream.

  • Key Manipulator: endl (End Line).
  • Function: When endl is encountered, it forces the cursor to move to the next line of the screen. All subsequent output will begin printing from that new line.
  • Usage: The endl manipulator must be appended to the cout statement using the Stream Insertion Operator (like, cout<<endl).

Example using endl:

# include <iostream>
using namespace std; 
int main{ 
    cout<<"Hello!"; 
    cout<<endl;
    cout<<"Codders"<<endl;
    cout<<"From 7Scribes."; 
    reutrn 0; 
}

  1. Output:
  2.             Hello!
  3.             Codders
  4.             From 7Scribes.
  5. Escape Sequences

An Escape Sequence is a sequence of characters that begins with a backslash () and performs a specific function, such as generating a space or a new line.

  • Key Escape Sequence for New Lines: n (New Line).
  • Function : Like endl, n forces the output to move to the next line. Their behaviors are functionally the same.
  • Usage: The n character must be placed within double quotes (like " n"). Since it is a character, it can be written directly inside the existing double quotes of the expression you are printing.

Example using n inside the string:

# include <iostream> 
using namespace std; 
int main{ 
    cout<<"Hello! n"; 
    cout<<"Codders n"; 
    cout<<"From 7Scribes";
    reutrn 0; 
}
  1. Output:
  2.            Hello!
  3.             Codders
  4.             From 7Scribes.

Note on Syntax: If your output expression is already inside double quotes, you do not need an extra stream insertion operator (<<) to add n. If the expression was not in quotes (e.g., a mathematical evaluation), you would need to use an extra stream insertion operator. Like cout<<10+4<<"n";.

 

Try It Yourself Challenge #2: Practice Pattern Printing > > Task: Write three separate cout statements that each print the following pattern. Ensure that each pattern appears on its own line using both endl and n in separate tests.

Pattern:

*

**

***

****

*****

Hint: You will need to use a line break command at the end of the first and second statements.

 

Summary of Key Concepts

Concept Simple Definition Example
Statement A single, complete instruction given to the computer. cout << "Hi";
Stream Object The mechanism (cout) that handles the flow of data to the screen. cout
Insertion Operator The symbol (<<) that pushes data into the stream. <<
Terminator The semicolon (;) that marks the end of an instruction. ;
Manipulator A built-in function to control output formatting. endl
Escape Sequence A backslash () followed by a character to perform a specific function (like a new line). n

 

Ready to Continue Your Journey?

Thank you for reviewing these comprehensive notes! Understanding statements and output is crucial for building functional C++ programs.

Next Steps:

  1. Watch the Video: If you haven't yet, watch the accompanying lecture video to see these concepts demonstrated live, and gives the final touch to your understanding of these topics.
  2. Practice: Complete the "Try It Yourself" challenges above. Practice using endl and n until you can reliably move output onto a new line.
  3. Future Reading: In the next lecture, we will explore how expressions that are not enclosed in double quotes are evaluated.

We want to hear from you! What is the most challenging concept for you when dealing with C++ output? Let us know in the comments below!

Keep reading and exploring new concepts. Happy Coding!