使用 cout 从 cin.get() 输出 int 类型

Using cout to output type int from cin.get()

#include<iostream>
using namespace std;

int main()
{
    int c;
    cout<<cin.eof()<<endl;

    while((c=cin.get())!=EOF)
    {
        cout<<c<<endl;
    }

    cout<<cin.eof()<<endl;
    return 0;
} 

代码如上所示。 这是输出。

我知道我应该使用 cout.put() 来输出结果。 但 我的问题是“10”是从哪里来的?

我的 ide 是 dev-c++ 5.11

10 是 ASCII 代码中的换行符。它来自输入输入后按回车键。

其实10是换行符'\n',它的十进制值为10,使用cin.get()时你会读到一个换行符和插入的换行符,因为回车关键,现在如果更新你的代码不再打印下面的 10:

while (c != EOF )
{
    c = cin.get();

    if (c != '\n')
    {
        cout << c << endl;
    }
}