cin
读取单个字符时候,cin将忽略空格和换行符。因此输入中的空格没有被回显,也没有包括在计数内。
发送给cin的输入被缓冲。这意味着只有在客户按下回车键后,他输入的内容才会发送给程序。
#include <iostream>
int main()
{
using namespace std;
char ch;
int count = 0; // use basic input
cout << "Enter characters; enter # to quit:\n";
cin >> ch; // get a character
while (ch != '#') // test the character
{
cout << ch; // echo the character
++count; // count the character
cin >> ch; // get the next character
}
cout << endl << count << " characters read\n";
return 0;
}
当输入字符串的时候,遇到空格、换行符和TAB的时候,会结束。
#include<iostream>
using namespace std;
int main(){
char a[20];
while(cin>>a){