简体   繁体   English

Cin和Cout对象如何循环工作

[英]how does the Cin and Cout objects work in a loop

I have this piece of code 我有这段代码

int main()
{
   char ch;
   while (cin >> ch)
     cout << ch;
   return 0;
}

What I'm wandering is how does cin work in the while() loop? 我在游荡的是cinwhile()循环中如何工作? I mean, does it have an inner index to were it left off? 我的意思是,它是否具有内部索引?

While you enter data, the loop will continue, it will only stops when find EOF (End of File) ctrl + C (in windows) ctrl + D (in linux) 输入数据时,循环将继续,仅当找到EOF(文件末尾) ctrl + C (在Windows中) ctrl + D (在Linux中)时,循环才会停止

This is usefull when you need to test a lot of cases and you don't know for sure how many are, you can enter how many times you want, the program will only stops when the end of file is found! 当您需要测试很多案例并且不确定是否有多少案例时,这很有用,您可以输入所需的次数,只有找到文件结尾时程序才会停止!

Example Input 输入示例

a
b
c
(ctrl + d)

Example Output 示例输出

a
b
c
the program will finish because EOF was found!

See this reference: http://www.cplusplus.com/reference/cstdio/EOF/ 请参阅此参考: http : //www.cplusplus.com/reference/cstdio/EOF/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM