C language
scanf
In addition to%c
outside input to other formats is a symbol of white space (spaces, TAB, Enter) marks the endTherefore,
scanf
unless%c
the character is read by a space, otherwise the space will be skipped%c
You can receive a space, tab, carriage returnUse string
%s
read in, is a space and line feed as read into the end markerUse
getchar
,putchar
input and output of a single charactergetchar
Can recognize line breaks, so when the knockout round if the latter havegetchar
will be put into your account
2#3
This input format, do not forget thescanf
inside to keep this formatgets
An input line of the string,puts
to the output string, and a line feed plus- If
gets
there's still ascanf
Orcin
, remember to usegetchar
absorption
- If
Utility output format
//%md 超过或等于m位保持原样,不足m位,高位用空格补齐 printf("%5d"); //%0md 超过或等于m位保持原样,不足m位,高位用0补齐!!!!!!非常好用 printf("%05d"); //%.mf 保留m位小数输出 printf("%.5f");
C++
Control output bits
#include<iomanip> //控制时要加入这个头文件
cout << setiosflags(ios::fixed)<<setprecision(2)<<123.4567<<endl;//控制输出位数
string
Only withcin,cout
input and output2#3
This input format may be a char additional variable, cin >> ch absorb itgetline
Whole line for inputting a character string- If
getline
there's still ascanf
Orcin
, remember to usegetchar
absorption
- If
//string类型
string str;
getline(cin, str);
//char *类型
char s[20];
cin.getline(s, 20);
- Used
map
, the character string to integer mapping muststring
Case
//输入格式:字符串1 字符串2 字符串3 ...数量未知
//输入格式:数字1 数字2 数字3 ...
//这种限于后面没有其他输入了
//这也是输入多组数据,组数未知的方式
while(scanf("%s")!=EOF){
}
/*
输入格式:
123 [数字]
aa bb cc [是一个整体的字符串]
字符串1 字符串2 字符串3 ... [组数未知的多组字符串]
1998 [数字]
*/
//c++
cin>>id;
getchar(); //注意!!
getline(cin,str1);
while(cin>>str2){
...
char ch = getchar();
if(ch == '\n') break; //注意!!
}
cin>>num;
//c语言
scanf("%d",&n);
getchar(); //注意!!
gets(str);
while(scanf("%s",str2)!=EOF){
...
char ch = getchar();
if(ch == '\n') break;
}
scanf("%d",&num);