Reading an unknow number of Inputs

来源:互联网 发布:奇爱博士讲电影知乎 编辑:程序博客网 时间:2024/04/30 07:03

take a look at the fllow program:

 

#include <iostream>

int main()

{

     int sum=0,value;

   while(std:cin>>value)

  {

       sum+=value;

  }

  std:cout<<"sum value is :"<<sum<<end;

reutnr 0;

}

 

The input operator return its left operand.The condition tests that result,meaning it tests std:cin.

 

When we use the iostream as a condition,the effect  is to test the state of the stream. if the stream is valid,that is ,if  it is still possible to read another input-then the test is successeds.

An istream becomes invalid when we hit end-of-file or encounter an invalid input,such as reading a value that is not an integer. An istream that is invalid state will cause the condition to fail.

 

Operating Systems uses different value for end-of-file.On Window Systems we enter a end-of-file by typing a control -z -simultaneousely type the "ctr" key and a "z".On Unix system ,includeing Mac -OS-X machines ,it is usually control-d.

 

原创粉丝点击