C++ Tutorial Lesson 3

input stream cin

Today, everyone can't wait to enter!

Let me show you first

#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a;
    return 0;
}

Type a number like me, press the Enter key, and you will find that the number you entered is output.

 

 We can make an improvement on it like:

#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a+5;
    return 0;
}
#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a-4;
    return 0;
}
#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a*2;
    return 0;
}
#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a/2;
    return 0;
}
#include<iostream>
using namespace std;
int main(){
    int a;//定义整数型变量a
    cin>>a;//输入a的值,注意中间为>>,不是<<
    cout<<a%7;
    return 0;
}

You can also do something fun like:

#include<iostream>
using namespace std;
int main(){
    int a,b;//定义整数型变量a
    cout<<"你好,我是你的加法机器人,输入2个数试试吧"<<endl;
    cin>>a>>b;//输入a的值,注意中间为>>,不是<<
    cout<<"答案是:"<<a+b;
    return 0;
}

run it,

 Well, that's it for today, next time I will expand the variables.

Guess you like

Origin blog.csdn.net/Djyt4102520/article/details/125524988