Site icon Learn C++

Easily Learn About Using Input And Output In C++ Programming

In general a developed applications, during it’s runtime, it do 3 actions; it gets inputs, do some logical algorithms and puts you outputs. If you read about Structure of C++ Programming Language, it is good to learn how input and output functions are used.

How do I use Outputs in C++?

In C++, cout object function is used with the << operator, direction of arrows shows the output and anything on the right side of this operator goes as an output. In C++ you must use with std namespace like std::cout. In C++ :: operator is used to refer members of a class or namespace, Here std is standard library which has many functions including input and output object functions. See example below,

[crayon-6622cdc951fbd811193537/]

Or you can use std as a namespace at the beginning and you don’t need to type std:: before using all of it’s objects. See example below;

[crayon-6622cdc951fc5396765091/]

Each cout object adds to previous input. You can use many of them, like this;

[crayon-6622cdc951fc7953563008/]

In this example output, all cout lines will be combined in one line and the output will be like this;

[crayon-6622cdc951fca223275670/]

this example is same with;

[crayon-6622cdc951fcb977921689/]

You can write each of them in one line by using << operator. See this example, it has same output as above too;

[crayon-6622cdc951fcd231018296/]

This method is easy to output variables. You can output texts and variables together as below,

[crayon-6622cdc951fcf352269899/]

So, how we can start from the next line, like when you press enter key. Here we use ‘\n’ character at the end or inside the string at the end, We can use endl which shows end of line too. All are same mostly \n is used, because endl See examples below

[crayon-6622cdc951fd2542346568/]

How do I use Inputs in C++?

In C++, cin object function is used with the >>  operator, direction of arrows shows the output is the variable and anything on the right side of this operator is input. In C++, if you don’t use namespace you must use with it’s std class like std::cout. In C++ :: is used to refer members of a class, Here std is namespace which has input and output object functions. See example below,

[crayon-6622cdc951fd4488124660/]

Or you can use std as a namespace at the beginning and you don’t need to type std:: before using all of it’s objects as given before. See example below;

[crayon-6622cdc951fd6280262623/]

This last example is also good to summarize all output and input mechanism in C++.

Head over and download Dev-C++ for modern C++ development on Windows.

Exit mobile version