c++获取键盘输⼊cin、scanf使⽤详解cin是c++标准,scanf是在c中使⽤的
1 #include<cstdio>
2 #include<iostream>
3 #include<cstring>
4
5using namespace std;
6
7int main()
8 {
9/*
10    strlen包含在string.h头⽂件⾥,加上
11    #include <string.h>
12    #include <cstring> // C语⾔头⽂件为 string.h 新c++编译器,前⾯加c ,后去掉 .h
13输⼊char[]
14*/
15char s[100100];
16    cin>>s;
17//strlen和sizeof的区别
18int len = strlen(s);
19int size = sizeof(s); //100100
20    cout<<s<<""<<len<<""<<size<<endl;
21
22//连续输⼊多个变量
23double z,g;
24int h;
25    cin>>z>>g>>h;
26    cout<<z<<""<<g<<""<<h<<endl;
27
28//输⼊string
29string str;
30    cin>>str;
31int len1 = str.length();
32int size1 = str.size();
33    cout<<str<<""<<len1<<""<<size1<<endl;
34
35/*
36    printf,scanf两个函数都包含在库⽂件<stdio.h>中。
37连续输⼊多个变量
38*/
39double x,y;
40int w;
41    scanf("%lf%lf%d",&x,&y,&w);
42    printf("%lf %lf %d",x,y,w);
43return0;
printf怎么加endl44 }
纯⽂本代码
#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
/*
strlen包含在string.h头⽂件⾥,加上
#include <string.h>
#include <cstring> // C语⾔头⽂件为 string.h 新c++编译器,前⾯加c ,后去掉 .h
输⼊char[]
*/
char s[100100];
cin>>s;
//strlen和sizeof的区别
int len = strlen(s);
int size = sizeof(s); //100100
cout<<s<<" "<<len<<" "<<size<<endl;
//连续输⼊多个变量
double z,g;
int h;
cin>>z>>g>>h;
cout<<z<<" "<<g<<" "<<h<<endl;
//输⼊string
string str;
cin>>str;
int len1 = str.length();
int size1 = str.size();
cout<<str<<" "<<len1<<" "<<size1<<endl;
/*
printf,scanf两个函数都包含在库⽂件<stdio.h>中。连续输⼊多个变量
*/
double x,y;
int w;
scanf("%lf%lf%d",&x,&y,&w);
printf("%lf %lf %d",x,y,w);
return 0;
}

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。