C++

Using the Std::Cout

Use the cout statement to see the output on the console in C++. It is an output stream object that is already declared and is a part of the iostream header file. By just employing the operator, you may utilize the cout to enter the output. Using the operator, multiple output statements can be chained together. By default, the cout output is shown on the console. But it can, if necessary, be sent to other output devices.

Implementation of Cout Statement in C++

A predefined output stream object called std::cout is used in C++ to show the output on the console. The syntax for std::cout is easy to understand:

The “Hello, World!” message is printed to the console by this syntax. The output is inserted into the stream using the operator.

Using the Cout Statement in C++

Now, we implement the very first and simple example where we learn about the cout statements in C++ programming language. The program begins by including the iostream header file which contains the definition of the cout object that we use to print the output to the console.

The program then declares four variables: “a” is an integer with the value of 10, “b” is an integer with the value of 5, “c” is a double with the value of 3.14159, and “d” is a character with the value of “A”. The << operator is employed to add each variable’s value into the output stream. We also include the string literals to provide a context for the values that we print out. We are also use the endl manipulator to insert a newline character at the end of each line of output.

#include <iostream>
using namespace std;

int main() {
   int a = 10, b = 5;
   double c = 3.14159;
   char d = 'A';
   cout << "The value of a is " << a << endl;
   cout << "The value of b is " << b << endl;
   cout << "The value of c is " << c << endl;
   cout << "The value of d is " << d << endl;
   return 0;
}

Finally, the main() function returns 0 which indicates to the operating system that the program is completed successfully. When this program is run, it outputs the following to the console:

Adding Two Numbers Using the Cout Stamtent in C++

In this instance, we declare two integer variables with the values of 10 and 20 for num1 and num2, respectively. The total of num1 and num2 is then printed using the cout command. The addition operation is carried out inside the cout statement using the + operator. Additionally, the string literals are used to give the output context. To guarantee that the output is written on a separate line in the console, the program utilizes the endl manipulator to insert a new line character at the end of the line of an output.

#include <iostream>
using namespace std;
int main() {
   int num1 = 10;
   int num2 = 20;
   cout << "The sum of " << num1 << " and " << num2 << " is: " << num1 + num2 << endl;
   return 0;
}

Here is the showcase of the program:

Printing the Price Before Tax, the Tax Rate, and the Total Price with Tax Using the Cout Statement

This C++ program demonstrates how to use cout to print out a formatted output that calculates the total price of an item with tax. The program first declares two double-precision floating-point variables: the price which represents the price of the item before tax, and the tax_rate which represents the tax rate as a decimal. The program then uses cout to print out three pieces of information.

First, the program uses the fixed manipulator to ensure that the floating-point numbers are printed with a fixed number of decimal places. Then, the setprecision() function is called with an argument of 2 to ensure that the numbers are printed with two decimal places. Next, the program prints out the price before tax using cout, along with a string literal that provides context for the output. The program then prints out the tax rate as a percentage using cout, along with a string literal that provides context for the output.

The tax rate variable is multiplied by 100 and is inserted into the output using the insertion operator <<. The % symbol is also included in the output string. Finally, the program calculates the total price with tax and prints it out using cout, along with a string literal that provides context for the output. The price variable is multiplied by (1 + tax_rate) to calculate the total price and then inserted into the output using the insertion operator <<.

#include <iostream>
#include <iomanip>
using namespace std;
int main() {
   double price = 19.99;
   double tax_rate = 0.0825;
   cout << fixed << setprecision(2);
   cout << "The price before tax is $" << price << endl;
   cout << "The tax rate is " << tax_rate * 100 << "%" << endl;
   cout << "The total price with tax is $" << price * (1 + tax_rate) << endl;
   return 0;
}

The following is the output of the program:

Outputing the Information About an Integer Variable and a String Variable Using the Cout Statement

This C++ program demonstrates how to use cout to output an information about an integer variable and a string variable. First, the program declares an integer variable entitled “num” and sets it to the value 42. Then, a string variable str is declared and initialized to the string literal of “Hello, world!”. The program then uses cout to output four lines of text to the console.

Each line of text includes a string literal and a value to be printed which are separated by the insertion operator <<. The first line of output prints the value of num using a string literal that includes the phrase “The value of num is”. The value of num is inserted into the output using << followed by the endl manipulator which adds a newline character to the end of the output. The second line of output prints the length of the str string using a string literal that includes the phrase “The length of the string is”.

The length() function is called on str to determine the length of the string, and the result is inserted into the output using <<. The third line of output prints the first character of the str string using a string literal that includes the phrase “The first character of the string is”. The character at index 0 of str is accessed using the subscript operator [], and the result is inserted into the output using <<. The fourth line of output prints the last character of the str string using a string literal that includes the phrase “The last character of the string is”. The character at index str.length()-1 of str is accessed using the subscript operator [], and the result is inserted into the output using <<.

#include <iostream>
using namespace std;
int main() {
   int num = 42;
   string str = "Hello, world!";
   cout << "The value of num is " << num << endl;
   cout << "The length of the string is " << str.length() << endl;
   cout << "The first character of the string is " << str[0] << endl;
   cout << "The last character of the string is " << str[str.length()-1] << endl;
   return 0;
}

Here is the output of the previously-implemented program:

Conclusion

We learned one of the basic statements of the C++ programming language which is the cout statement. We implemented multiple examples so that we can easily understand the working of cout statements in the C++ programming language.

About the author

Omar Farooq

Hello Readers, I am Omar and I have been writing technical articles from last decade. You can check out my writing pieces.