14.6 C
Jaipur
Wednesday, December 1, 2021

C++ Cout Format

Must read

Within the C++ language, there are some objects specified in libraries to do some specific tasks. Just like this, the iostream library has been used to input and output the data to an input and output device while using C++. The “cout” object is the main object used to display the data to a device screen while including the “iostream” header file in the code. Let’s have some examples to see the format of the C++ cout statement in Ubuntu 20.04.

Example 01

Let’s have our first illustration. Do create a C++ file while using the “touch” instruction on the shell. This file will be automatically created in the HOME directory of the Linux environment. Make sure that some editor has been installed on your Linux system i.e. nano, vim, text editor. We have been using the “GNU Nano” editor to open and edit our newly-created C++ file.

Now the file is launched in the editor pad within the shell. Start the C++ code by adding the input-output stream header at the first line. You need to add the standard namespace to make your code executable. After this, the main() method is initialized.

We will be using the “cout” statement to print out some string values. The “cout” statement is followed by “<<” signs and a string within the inverted commas ends at the semicolon “;”. This is the simplest format of the “cout” statement. Use “Ctrl+S” and “Ctrl+X” to save and exit the file.

Make sure your system has a “g++” compiler configured already. So, we have been utilizing the “g++” compiler to compile our file “cout.cc”. If the compilation doesn’t show anything, it means the code is correct.

To run this code, add the “./a.out” query after the compilation command in the shell. The output shows the string has been printed out at the shell as it is.

Example 02

From the above “cout” format, the string sentence has been printed out while the space of 1 line has not been given at the end of it. To make your output more precise, open the file to update it. Now, you just have to add the “n” special sign character within the string in the “cout” statement to give a newline space after the string.

Let’s compile the updated file. The compilation is successful and we have executed the code with the “./a.out” instruction. The output is showing the same string with a newline at the end.

Example 03

Let’s make use of another thing in the “cout” format to achieve the new line at the end of a string. Open your same C++ file “cout.cc” once again with the help of a “nano” instruction. Don’t try to change any line in the code as it is not required yet. The only change we require here is at the “cout” output line.

We have added the “<<” special characters along with the word “endl” stating the end line. This is a standard way to let our system compiler know that this is the end of the line and you have to jump at another line.

Compile your file once again to apply the updates with the “g++” compiler. After running the file, we got the same results as we got using the “n” in the string. Although it is not different at all, the use of “endl” is a more standard way than the “n” to add a new line.

Example 04

Let see how the “cout” stream would be working on the integer type values. So, make your file up to date by changing it a little. We have opened the file and added another “cout” line after the first “cout” line with the string. The second “cout” line contains the numerical value i.e., 300 to be outputted on the shell.

You can see that the standard format of “cout” has been used here i.e., with endl. Due to the “endl” used at the end of the “cout” line, both the output lines will be displayed separately at a new line. Otherwise, it won’t have happened like that.

Here comes the compilation once again. Use the same old query of the “g++” compiler to compile the updated code as demonstrated below. After the compilation of the file is successful i.e., free of errors, then run it using the “./a.out” command within the console application. The output is something like shown in the image attached. The string sentence has been displayed first then the integer type values have been displayed as expected.

Example 05

Let’s see another example to have a glance at the output of the “cout” stream on some variables. At the start of the main method, declare an integer type variable “z” and initialize it with some integer value i.e. 5000. We have also added another “cout” streamline to display this variable value.

When you define some variable within the C++ code, you don’t need to add its value in the “cout” line to display. You have to only specify the variable in the “cout” stream to display its value. We did the same. The third “cout” line is specified with the variable “z”.

Compile the updated file and run once again. You can see it displays the line of data as its output. The first two lines are the same as in the above example. The last line is displayed as the value of a variable “z”.

Example 06

You can also use the string and variable in the same “cout” format to display both at the same time. It is a very efficient and cool method to get output. After opening the file, we have added only a single “cout” line. This line contains a string and a variable to be outputted on the shell.

Compile the code once again. Then, run it with the “./a.out” query as shown in the below image. The output is shown in the image attached beneath.

Example 07

Let’s have a look at our last example of the “cout” format discussion in C++. This example will show you to display more than 1 variable value in the shell along with some string text within the “cout” format. Open the file the last time and declare and initialize an integer type variable “age” with some value in it. Declare and initialize another string type variable named “name” and assign it some string value. Then, a “cout” stream has been used here to print both the variables in the shell along with some string in it as well.

After compiling and running the updated code, we have got a single line output on the shell screen. The word “Aqsa” and “25” is the variable value while others are just some strings.

Conclusion

This article contains several examples to illustrate the different formats of using the “cout” stream within the C++ language. We have used the “n” special character and “endl” keyword in the “cout” statement to make a different format and output. Also, the use of integer and string variables in the “cout” clause is discussed. To sum up, this article is a bonus bundle for Linux users who want to know the basics of the C++ cout format.

Source link

- Advertisement -

More articles

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisement -

Latest article