C++ 中如何判断cin输入流的结尾
大家讲道理
大家讲道理 2017-04-17 11:08:08
[C++讨论组]
string tmp;
while(1)
{
cin >> tmp;
if(tmp[0] == '\0')
break;
cout << "输出字符:" << tmp << endl;
}

我使用了 cin.eof()cin.good() 都不可以,在win7平台下,mingw编译 我刚刚开始学习C++,希望高人帮帮忙

原来是我自己的思路想错了,不能直接判断cin是否到结尾,应该输入Ctrl+z 再按回车,这样就能退出了

大家讲道理
大家讲道理

光阴似箭催人老,日月如移越少年。

全部回复(2)
巴扎黑
#include <iostream>
#include <string>
using namespace std;
int main(){
        string tmp = "";
        while(cin >> tmp){
                cout << "输出字符:" << tmp << endl;
        }
        return 0;
}

Here the expression cin >> x performs an input operation that might update x, and as its expression result returns a reference to the stream, i.e. to cin. So cin is being used directly as a condition. That invokes a conversion to boolean, which is defined such that it on its own is equivalent to !cin.fail() (i.e., the expression cin >> x as condition is equivalent to writing !(cin >> x).fail() or, as a comma expression, (cin >> x, !cin.fail())).

http://stackoverflow.com/questions/13343991/c-why-cin-eof-read-last-char-twice

PHPz

while( cin >> tmp ) cout << tmp << endl ;

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号