循环和文本输入(二):使用cin.get(ch)进行输入

如下代码:
#include 

using namespace std;
int main(int argc, const char * argv[]) {
    char ch;
    int count = 0;
    cout << "Please Enter The Char String : " << endl;
    cin.get(ch);
    while (ch != 'q') {
        cout << ch;
        ++count;
        cin.get(ch);
    }
    cout << endl << "All number is : " << count << endl;
    return 0;
}

说明:

1.cin输入逐个读取字符,但会像读取其他数据一样自动 忽略空格和换行符;
2.cin.get(ch)是读取输入中的下一个字符,包括空格和换行符,同时也不会忽略它们;
3.调用cin.get()时传递的是ch,而不是&ch;但是,在c语言中,调用函数修改变量的值,必须将变量的地址传递给函数而不是变量本身。

你可能感兴趣的:(循环和文本输入(二):使用cin.get(ch)进行输入)