HomeLinuxC++ Cout Format

C++ Cout Format


In C++, the cout object is an object of the iostream class. It’s specified within the iostream header file. It’s linked to the everyday output system which is mostly a console. For the output to be exhibited on a display screen, the cout is utilized with the stream insertion operator (<<).

Syntax:
The syntax of cout is as follows:

cout << variable;
or
cout << “string”;

Now, utilizing this syntax, we carry out the assorted illustrations on this article.

Utilizing Cout to Show the Character String

On this illustration, we display the simplest and easiest option to make the most of the cout object. We first embody the header recordsdata after which make the most of the cout object inside the principle() perform to show a string of characters on the console.

#embody <iostream>
utilizing namespace std;
int major()
{
    cout << “It is a cout format information.”;
    return 0;
}

This system begins with the inclusion of the required library which is “iostream”. Then, we use the “namespace std” library. This library permits us to make use of all of the capabilities, variables, and lessons which might be out there beneath the usual library namespace. The primary() perform begins and we use the cout object with the insertion operator inside its braces. Then, we specify the textual content that we have to show between the double citation marks as “It is a cout format information.” The return “0” signifies that this system ran efficiently.

The next snapshot reveals that the supplied textual content is displayed on the console:

Utilizing Cout to Print the Integer Worth that Is Saved in a Variable

One other approach to make use of the cout in C++ to show the output is to initialize a variable first. Then, present it to the cout object to print it on the console. On this instance, we initialize an integer kind variable with “a” worth. Then, it’s displayed on the console utilizing the rely object.

#embody <iostream>
utilizing namespace std;
int major()
{
  int s = 1000;
  cout << “The worth of s is “ << s;
  return 0;
}

First, we embody the “iostream” header file. Then, the std namespace library is used to make the execution of the error-free code. The primary() perform is initialized and we initialize a variable “s” inside the principle() perform with int datatype and assign it with the worth of 1000.

Now, to show the worth of this variable, we use the cout object with the insertion operator after which specify the variable identify. We will additionally show a textual content string with the cout object by writing it in between the double citation marks after the insertion operator. The cout merely prints no matter is outlined between the double citation marks as it’s on the console.

Right here, we outline it as “The worth of s is” adopted by the insertion operator. Then, we outline the identify of the variable whose worth needs to be displayed. Lastly, put a semicolon(;) to find out the top of an announcement.

The output picture reveals the supplied textual content string with the worth which is saved in variable “s”:

Utilizing Cout to Show the Sum of Two Numbers

Other than displaying the values on the console, the cout object will also be used to carry out arithmetical operations. We will merely specify the operation within the rely assertion and the calculated output is displayed on the console.

#embody <iostream>
utilizing namespace std;
int major() {
    int p;
    int r;
    cout << “Enter first quantity:”;
    cin >> p;
    cout << “Enter second quantity:”;
    cin >> r;
    cout << “The primary quantity is: “ <<p<<endl;
    cout << “The second quantity is:” <<r<<endl;
    cout << “The sum of p and r is:”<<p+r<<endl;  
    return 0;
}

After together with the required libraries, we enter the principle() perform of this system. Right here, we declare two variables – “p” and “r” – each having the integer datatype. Now, we take the enter values for each these variables from the consumer, one after the other. A cout object is used to show the textual content “Enter first quantity:” on the display screen. Then on the following line, we use the cin object which takes the enter from the consumer and shops the user-provided quantity within the “p” variable.

After that, we show one other textual content “Enter second quantity:” with using cout. Cin takes the enter worth for the “r” variable.

As you possibly can see within the snapshot, the consumer enters the primary quantity and hits “Enter”. The management asks the consumer to enter a second worth. Right here, the primary quantity that’s entered by the consumer is 20 and the second quantity is 7.

We then exhibit each the quantity on the console. To try this, we merely add a textual content and the variable identify to the cout object with using the insertion operator. On the finish of every cout assertion, we use the “endl” perform which instantly provides a brand new line after the next code.

Now, to rely the sum of those two numbers, we write the athematic operator “+” between each the variables “p” and “r” together with the “The sum of p and r is:” assertion. As this system is executed, the cout object calculates the sum of values which might be saved in these variables and shows it on the console with the required textual content.

The next picture reveals each the entered values and the sum of those two values:

Utilizing Cout to Show the Character and Integer Variables in a Single Line

We will show a number of variables with a single cout assertion. Right here is this system to implement this method:

#embody <iostream>
utilizing namespace std;
int major()
{
    string Identify = “Harry”;
    string Nation = “America”;
    int Age = 28;
    cout << “My Identify is “ << Identify <<” , my age is “<< Age << ” and I reside in “ << Nation << endl;
    return 0;
}

On this program, we initialize two strings and one int worth. The primary string is “Identify” which is initialized as “Harry”. The second string is “Nation” which is given with the worth of “America”. Lastly, we’ve the int variable “Age” with the worth of “28”. Now, we use the cout object with the insertion operator to show all these values in a single line.

First, we write the cout object adopted by the insertion operator. Then, with the assistance of double citation marks, we outlined the “My identify is” textual content. Then, the variable that shops the identify is added after the insertion operator, “Identify”. By following the identical sample, we add the opposite two variables with the required textual content on this cout assertion. Lastly, the execution of this system offers us a single-line output.

The output assertion consists of all three variables with the textual content and is displayed on the console:

Conclusion

The C++ cout format is outlined and defined on this article. We supplied you with the syntax to make use of this object. Numerous illustrations are demonstrated virtually with a radical exaplanation of their particulars. We first confirmed you the simplest and easiest format to make use of a cout assertion. Additionally, we carried out an instance in calculating the sum of two variables in cout. Every instance is elaborated on the completely different codecs which might be used for the cout object in C++.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments