Skip to main content

Learn C++ Input and Output



5.1 cout (output stream)


On most program environments, the standard output by default is the screen, and the C++ stream object defined to access it is cout. cout is an instance of iostream class


For formatted output operations, cout is used together with the insertion operator, which is written as << (i.e., two "less than" signs).


cout << "this is Output";

// prints this is Output sentence on screen

cout << 50;

// prints number 50 on screen

cout << x;

// prints the value of x on screen


The << operator inserts the data that follows it into the stream that precedes it. In the examples above, it inserted the literal string Output sentence, the number 120, and the value of variable x into the standard output stream cout. Notice that the sentence in the first statement is enclosed in double quotes (") because it is a string literal, while in the last one, x is not. The double quoting is what makes the difference; when the text is enclosed between them, the text is printed literally; when they are not, the text is interpreted as the identifier of a variable, and its value is printed instead.


For example, these two sentences have very different results:

cout << "Hello"; // prints Hello

cout << Hello; // prints the content of variable Hello


Multiple insertion operations (<<) may be chained in a single statement:

cout << "This " << " is a " << "single C++ statement";


This last statement would print the text This is a single C++ statement. Chaining insertions is especially useful to mix literals and variables in a single statement:

cout << "I am " << age << " years old and my zipcode is " << zipcode;




5.2 cin (input stream)


In most program environments, the standard input by default is the keyboard, and the C++ stream object defined to access it is cin. cin is an instance of iostream class


For formatted input operations, cin is used together with the extraction operator, which is written as >> (i.e., two "greater than" signs). This operator is then followed by the variable where the extracted data is stored. For example:

int age;

//declares a variable of type int called age

cin >> age;

//extracts a value to be stored in it


This operation makes the program wait for input from cin; generally, this means that the program will wait for the user to enter some sequence with the keyboard.


Extractions on cin can also be chained to request more than one datum in a single statement:

cin >> a >> b;


This is equivalent to:

cin >> a;

cin >> b;

In both cases, the user is expected to introduce two values, one for variable a, and another for variable b. Any kind of space is used to separate two consecutive input operations; this may either be a space, a tab, or a new-line character. 






5.3 cerr (error stream)


The predefined object cerr is an instance of iostream class. The cerr object is said to be attached to the standard error device, which is also a display screen but the object cerr is un-buffered and each stream insertion to cerr causes its output to appear immediately.


The cerr is also used in conjunction with the stream insertion operator as shown in the following example.

int main( )

{

char str[] = "Unable to read....";

cerr << "Error message : " << str << endl;

}


When the above code is compiled and executed, it produces the following result:

Error message : Unable to read....




5.4 clog (log stream)


The predefined object clog is an instance of ostream class. The clog object is said to be attached to the standard error device, which is also a display screen but the object clog is buffered. This means that each insertion to clog could cause its output to be held in a buffer until the buffer is filled or until the buffer is flushed.


The clog is also used in conjunction with the stream insertion operator as shown in the following example.

int main( )

{

char str[] = "Unable to read....";

clog << "Error message : " << str << endl;

}


When the above code is compiled and executed, it produces the following result:

Error message : Unable to read....


You would not be able to see any difference in cout, cerr and clog with these small examples, but while writing and executing big programs then difference becomes obvious. So this is good practice to display error messages using cerr stream and while displaying other log messages then clog should be used.




 

Comments

Popular posts from this blog

How To install Kali Linux (nethunter) On Android (Without root) via proot-distro and install Kali Linux Xfce Desktop On Android using Termux

What is kali linux? Kali Linux is a Debian-derived Linux distribution designed for digital forensics and penetration testing. It is maintained and funded by Offensive Security , This tutorial isn't only about learning how to install Kali Linux. Okay let's run down what we will be doing                        Table Of Content •Installing Kali Linux Terminal •Installing Kali Linux Desktop •Installing Default packages  •Creating vncserver port •Closing vncserver port •Closing multiple vncserver ports  •Reseting Kali Linux •Uninstalling Kali Linux  Okay!!!! Let's Begin Requirements: • Termux  : Click Here To Download Termux   Installation:   Please you can also copy and paste the commands below    The First thing we have to do is to make sure our repositories are up to date, to do that type the command  pkg update   It is essent...

How To Create a Strong Virus And A Payload with Python 2023

Let’s start by saying that viruses are a little bit anachronistic in 2023… nowadays other kinds of  malware (like worms for example:) are far more common than viruses. Moreover, modern operative systems are more secure and less prone to be infected than MS-DOS or Windows 95 were (sorry Microsoft…) and people are more aware of the risk of malware in general. Moreover, to write a computer virus, probably Python is not the best choice at all. It’s an interpreted language and so it needs an interpreter to be executed. Yes, you can embed an interpreter to your virus but your resulting virus will be heavier and a little clunky… let’s be clear, to write a virus probably other languages that can work to a lower level and that can be compiled are probably a better choice and that’s why in the old days it was very common to see viruses written in C or Assembly. That said, it is still possible to write computer viruses in Python, and in this article, you will have a practical demonstration. T...

How To install Windows 10 in Termux(Without rooting your Android phone)

   Hello guys what's up, Today we are going to see how easily we can run Windows 10 Operating System on Android Phone just by running few commands on Termux and if you will like this post I will also show you how to run the Windows 8.1 and Kali Linux GUI(Without root) on Android using Termux only in an upcoming post so just comment down below if you are interested. This method is quite interesting as this will make you understand how powerful a Linux terminal can be so without wasting any more time let’s start this tutorial. Below you will find all the resources you need and their download links so Download all the files, install, and copy it to your smartphone's Internal or External Storage. Requirements: •ZArchiver : Click Here To Download ZArchiver • Termux  : Click Here To Download Termux • AVNC :  Click Here To Download AVNC • Windows 10 : Click Here To Download The Windows 10 File After download the windows 10 file you need to Install termux and AVNC server...