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?
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?
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/