'How do I remove EOF in cin buffer?

I'm using an example to show what I mean: On my Mac, to leave the first "while loop", the user has to type "CTRL+D". From what I understand, that is achieved by adding an EOF into the cin buffer, thus "c >> x" returns false. Now, I intend the user to enter the second "while loop" after leaving the first loop. How do I clear the EOF from the cin buffer, so that "cin >>" y returns true?

using namespace std;
int main()
{
    int x;
    int y;
    while(cin >> x)
    {
        cout << x << endl;
    }
    
    if(cin.get()==EOF) cout << "yes" << endl;
    while(cin >> y)
    {
        cout << y*y << endl;
    }
    return 0;
}


Solution 1:[1]

Maybe it's late to answer, but for others who might have a similar situation like mine; I have to handle EOF signal while prompting user and still need stdin later in the program. One way to go about it is to do fork/wait (a general example), so that the piece of code where EOF is expected can be executed by another child process and then terminate without effecting stdin of the parent process.

I've tried before other solutions without success i.e std::cin.clear(). The stdin of your program should be shutdown, if EOF is signaled -- usually via CTRL+D (unix) or CTRL+Z (windows).

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1