1. 程式人生 > >c++獲取鍵盤輸入cin、scanf使用詳解

c++獲取鍵盤輸入cin、scanf使用詳解

pan microsoft nbsp stdio.h c語言 多個 鍵盤輸入 獲取鍵盤輸入 int

cin是c++標準,scanf是在c中使用的

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 
 5 using namespace std;
 6 
 7 int main()
 8 {
 9     /*
10     strlen包含在string.h頭文件裏,加上
11     #include <string.h>
12     #include <cstring> // C語言頭文件為 string.h 新c++編譯器 ,前面加c ,後去掉 .h
13 輸入char[] 14 */ 15 char s[100100]; 16 cin>>s; 17 //strlen和sizeof的區別 18 int len = strlen(s); 19 int size = sizeof(s); //100100 20 cout<<s<<" "<<len<<" "<<size<<endl; 21 22 //連續輸入多個變量 23 double z,g; 24 int h; 25 cin>>z>>g>>h;
26 cout<<z<<" "<<g<<" "<<h<<endl; 27 28 //輸入string 29 string str; 30 cin>>str; 31 int len1 = str.length(); 32 int size1 = str.size(); 33 cout<<str<<" "<<len1<<" "<<size1<<endl; 34 35 /*
36 printf,scanf兩個函數都包含在庫文件<stdio.h>中。 37 連續輸入多個變量 38 */ 39 double x,y; 40 int w; 41 scanf("%lf%lf%d",&x,&y,&w); 42 printf("%lf %lf %d",x,y,w); 43 return 0; 44 }

純文本代碼

#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;
//strlensizeof的區別
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;
}

c++獲取鍵盤輸入cin、scanf使用詳解