1未知输入个数
如输入
1 2
3 4
可采用如下方式
c语言
while(scanf("%d%d",&a,&b)!=EOF){
//可执行代码
}
========================================
c++
while(cin>>a>>b){
//可执行代码
}
若是想中途退出则将判断值读入,为真直接break
2字符串输入
c语言
char s[100];//超出输入字符串长度
scanf("%s",&s);//遇到空格结束(不包括空格)
printf("%s",s);
输入:1231
输出:1231
=============================================
c++
方法1.cin
头文件<string>
string s;
cin>>s;
cout<<s;
输入:1231
输出:1231
方法2.cin.get()
char s[100];
cin.get(s,10);//10为输入个数,最多9个字符加'\0'
cout<<s;
输入:1234567890
输出:123456789
方法3.cin.getline()
char s[100];
cin.getline(s,10,'\n'); //s为字符数组,读取个数为10-1=9末尾为'\0','\n'为读到该字符停止读入(不包含该字符)
cin.getline(s,10);//默认为'\n' //getline会设置失效位,关闭后面的输入即字符后读不到
cout<<s<<endl;
输入:123456789000000
输出:123456789
方法4.getline() 头文件<string>
string s;
getline(cin,s);
cout<<s;
输入:12345 678 90
输出:12345 678 90
方法5.gets() 头文件<string>
char s[100];
gets(s);//可读空格,会将末尾换行符换为'\0'
cout<<s;
输入:12345 678 90
输出:12345 678 90
3不同进制数输入
c语言
int a,b,c;
scanf("%o%d%x",&a,&b,&c);//16进制10进制8进制
printf("%o%d%x",a,b,c);
==========================================
c++
int a,b,c;
cin>>hex>>a;//十六进制
cin>>oct>>b;//八进制
cin>>dec>>c;//十进制
cout<<hex<<a<<endl;
cout<<oct<<b<<endl;
cout<<dec<<c<<endl;
输入:c 12 12
输出:c 12 12