通过while循环显示输入的字符并计算总字符数
1.cin>>ch
此方式忽略空格和换行符
<span style="font-family:SimSun;font-size:12px;">#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
char ch;
cout<<"Enter characters:enter # to quit:\n";
int count = 0;
cin>>ch;
while (ch!='#')
{
++count;
cout<<ch;
cin>>ch;
}
cout<<endl<<count<<"characters read\n";
system("pause");
return 0;
}
</span>
2.cin.get(ch)此方式回显每个字符,并将全部字符计算在内
<span style="font-family:SimSun;font-size:12px;">#include "stdafx.h"
#include <iostream>
int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;
char ch;
cout<<"Enter characters:enter # to quit:\n";
int count = 0;
cin.get(ch);
while (ch!='#')
{
++count;
cout<<ch;
cin.get(ch);
}
cout<<endl<<count<<"characters read\n";
system("pause");
return 0;
}
</span>
3.cin.get()和cin.get(name,ArSize)(数组名(字符串(char*类型)的地址),int类型的整数)
函数重载让cin.get以不同方式执行相同的任务,只需要换参数列表,编译器自动选择需要的函数版本