1

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? I mean, does it have an inner index to were it left off?

TriskalJM
  • 2,393
  • 1
  • 19
  • 20
AnotherOne
  • 854
  • 2
  • 8
  • 19
  • ``cin`` works like a buffer. When you input data it gets stored in a queue. When you read data from ``cin`` the data in the queue or buffer (however you want to call it) gets removed. – BrainStone Mar 21 '16 at 22:33
  • An inner index? Why yes it does! Read more here: http://en.cppreference.com/w/cpp/io/basic_istream/tellg – user4581301 Mar 21 '16 at 23:28

1 Answers1

2

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)

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/

Han Arantes
  • 775
  • 1
  • 7
  • 19