Testing for end of input[转]

转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://huahualala.blogbus.com/logs/10078056.html

3.1.1 Testing for end of input

Conceptually, the only really new part of this program is the condition in the while statement. That condition implicitly uses an istream as the subject of the while condition:

while (cin >> x) {/*...*/}

 

The effect of this statement is to attempt to read from cin. If the read succeeds, x will hold the value that we just read, and the while test also succeeds. If the read fails (either because we have run out of input or because we encountered input that was invalid for the type of x), then the while test fails, and we should not rely on the value of x.

Understanding how this code works is a bit subtle. We can start by remembering that the >> operator returns its left operand, so that asking for the value of cin >> x is equivalent to executing cin >> x and then asking for the value of cin. For example, we can read a single value into x, and test whether we were successful in doing so, by executing

if (cin >> x) {/*...*/}

This statement has the same meaning as

cin >> x;
if (cin) { /* ... */ }

When we use cin >> x as a condition, we aren't just testing the condition; we are also reading a value into x as a side effect. Now, all we need to do is figure out what it means to use cin as a condition in a while statement.

Because cin has type istream, which is part of the standard library, we must look to the definition of istream for the meaning of if (cin) or while (cin). The details of that definition turn out to be complicated enough that we won't discuss it in detail until §12.5/222. However, even without these details, we can already understand a useful amount of what is happening.

The conditions that we used in Chapter 2 all involved relational operators that directly yield values of type bool. In addition, we can use expressions that yield values of arithmetic type as conditions. When used in a condition, the arithmetic value is converted to a bool: Nonzero values convert to true; zero values convert to false. For now, what we need to know is that similarly, the istream class provides a conversion that can be used to convert cin into a value that can be used in a condition. We don't yet know what type that value has, but we do know that the value can be converted to bool. Accordingly, we know that the value can be used in a condition. The value that this conversion yields depends on the internal state of the istream object, which will remember whether the last attempt to read worked. Thus, using cin as a condition is equivalent to testing whether the last attempt to read from cin was successful.

There are several ways in which trying to read from a stream can be unsuccessful:

  • We might have reached the end of the input file.
  • We might have encountered input that is incompatible with the type of the variable that we are trying to read, such as might happen if we try to read an int and find something that isn't a number.
  • The system might have detected a hardware failure on the input device.

In any of these cases, the effect is the same: Using this input stream as a condition will indicate that the condition is false. Moreover, once we have failed to read from a stream, all further attempts to read from that stream will fail until we reset the stream, which we'll learn how to do in §4.1.3/57.

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值