Self-cultivation for C++ lovers (1): Simple input and output

Today, the editor will share the simple input and output of C++ (cout and cin are introduced here, not printf and scanf in the cstdio library)

First of all, to use the input and output functions, the iostream library must be referenced :

#include<iostream>

1. Output

The code used for output is cout, and its format is as follows:

cout<<"direct output content";

cout<<variable name;

cout<<single character;

You can also:

cout<<a<<b<<c<<d<<e<<···<<m<<n; (a,b,c,d······· can be any of the above )

Note: The stream output operator (<<) should not be confused with the stream input operator (>>)!

For example:

#include<iostream>
using namespace std;
int main(){
    cout<<"你好,我是喜欢电脑的平某人";
    return 0;
}

running result:

And if you want to wrap, enter this line of code:

cout<<endl;

or:

cout<<"\n";

2. Input

The code used for the output is cin, which has the following format:

cin>>variable name;

You can also:

cin>>a>>b>>c>>d>>e>>···>>m>>n; (a, b, c, d······· can be any of the above )

Tips: Enter multiple variables here and separate them with spaces!

Note: The stream output operator (<<) should not be confused with the stream input operator (>>)!

For example:

#include<iostream>
using namespace std;
int main(){
    int a;
    cin>>a;
    cout<<a;
    return 0;
}

running result:

Enter an integer variable a (-32768<a<32767), and it will output the content just entered

The above is today's knowledge sharing~

Remember to like it!

Guess you like

Origin blog.csdn.net/pyz258/article/details/129389551