Topic 2 Output Operations
Output operations are fundamental in programming as they enable programs to display
information to the user. In C++ programming, displaying output to the screen or console
can be achieved using the cout stream. This guide will cover the basics of output operations
in C++, including displaying output and using cout for formatted output.
Displaying Output to the Screen/Console
To display output to the screen or console in C++, you can use the cout stream, which is part
of the Standard Library and defined in the <iostream> header. It allows you to output text,
variables, and other data to the screen. The cout stream is an instance of
the std::ostream class and is used for output operations.
Here's a simple example:
In the above code, the cout stream is used to display the string "Hello, world!" to the screen.
The << operator is used to insert data into the cout stream, and std::endl represents a
newline character, which moves the cursor to the next line.
Using \n for Newlines
In C++, the \n character is used to insert a newline in the output. This special character
moves the cursor to the beginning of the next line, allowing you to format your output text
neatly. While std::endl is often used, \n can be a more efficient option when you don't need
to flush the output buffer.
Example:
In the above example, the \n character is used within the strings to create new lines. The
output will be:
Using cout for Formatted Output
One of the powerful features of the cout stream is its ability to format output. This allows
you to control the appearance of the displayed data, such as specifying the number of
decimal places for floating-point numbers or padding strings with spaces. Manipulators are
used with the cout stream to specify formatting options.
Here's an example that demonstrates the use of manipulators for formatted output:
In the above code, std::fixed and std::setprecision(2) are manipulators used to specify the
number of decimal places for the num2 variable. The output will display the values of the
variables in the specified format.
Conclusion
Output operations are crucial for displaying information to the user in a C++ program. By
using the cout stream and manipulators, you can display output to the screen or console
and format the output according to your requirements. Understanding how to
use cout effectively allows you to present data in a readable and meaningful way to users.
References
C++ Input/Output with iostream
C++ Manipulators for Formatting Output
C++ Standard Library cout Documentation
Activity 2:
1. Write a program that generates a pattern of stars, ascending in quantity, as depicted
below: Star Ascension
*
**
***
****
*****
Sample Output 1
*
**
***
****
*****
2. Write a program that brings the vibrant beauty of a rainbow to life by gracefully printing
a colorful arch of hues. Copy the number of spaces before each color to mimic the arch of
the rainbow as depicted below: Rainbow Arch
Green
Yellow Brown
Orange Indigo
Red Violet
Sample Output 1
Green
Yellow Brown
Orange Indigo
Red Violet
3. Write a program that showcases a pattern of the word "CODE" using the letters C, O, D,
and E. Code Symphony
Sample Output 1
CCCCC OOOOO DDDDD EEEEE
C O O D D E
C O O D D EEE
C O O D D E
CCCCC OOOOO DDDDD EEEEE
4. Write a program that greets the user by displaying the message "Hello, World!" on the
console. Displaying a Message
Sample Output 1
Hello, World!
5. Write a program that prints the first 5 even numbers, beginning from 2, with each
number occupying its own line. Even number parade
Sample Output 1
2
4
6
8
10