In addition to being fully compatible with the C writing method, which is implemented using the printf and scanf functions, C++ also has its own set of input and output streams (the stream mentioned here refers to reading from a certain IO device or written sequence of characters, customarily called a " stream "). The input and output streams in C++ are represented by cin and cout respectively. Before using it, you need to use the standard library iostream, that is, you must also add the sentence #include<iostream> at the beginning. Let me show you how to use cin and cout.
1. Use of cout output stream:
The cout output stream needs to be used with the << output operator , such as the output statement:
cout<<Hello;
The string Hello will be displayed on the screen.
Essentially, the string Hello is inserted into the cout object, and the cout object is returned as the return value, so you can also use << to output multiple contents continuously later, such as:
cout<<Hello<<www.dotcpp.com;
Hello www.dotcpp.com will appear on the screen.
You can learn and experiment on your own!
In addition, when it comes to cout, the most commonly used one is the endl manipulator, which can be directly inserted into cout to produce a newline output, such as:
cout<<Hello<<endl<<www.dotcpp.com<<endl;
The screen will display:
Hellowww.dotcpp.com
Please be sure to test it on the computer and try to use cout output.
2. Use of cin input stream:
Before receiving a piece of data, you must first define a variable of the same type to store the data, and then use cin with the >> input operator to receive user input from the keyboard, such as code:
#include<iostream>usingnamespacestd;intmain(){inta;cout<<inputnumber:<<endl;cin>>a;cout<<Get<<a<<endl;return0;}
After the program is run, the results are as follows:
inputnumber:24Get24
Similarly, cin can also receive multiple variables continuously, such as:
inta,b;cin>>a>>b;
Please test the use of cin and cout by typing the code yourself!
Through the above learning, I believe everyone has initially mastered the use of input and output in C++. You may be a little uncomfortable with it. For example, you do not need to specify the variable type of input or output. This is because cin and cout themselves are a class, not a function. , and even keywords, everyone must be aware of this. And with continued in-depth study in the future, everyone will gradually understand the reason.