Q:

cin.get() is not getting out of loop

I am using the following code:
#include <stdio.h>
#include <iostream>
using namespace std;

int main ()
{
    char c ;
    c = cin.get() ;
    do {
        cout.put(c) ;

        c = cin.get() ;
    } while ( !cin.eof()) ;

    cout << "coming out!" << endl;
    return 0;
}

Problem with the above code is, its not getting out of loop, which means its not printing "coming out". Anybody can help why it is so? I am testing this program on mac and linux.
Thanks

A:

It does print "coming out" provided that it gets the end-of-file. It will go out of the loop if you redirect a file to it with
./program < file

or send the end-of-file yourself, by hitting ctrl+d (linux) or ctrl+z (dos)

A:

Standard in never hits EOF, unless you press Ctrl+Z.
Therefore, cin.eof() is always false.
To fix it, press Ctrl+Z to send an End-Of-File character.
Alternatively, you could change the condition.  (eg, while(c != '\n'))

