Stop endless loop by pressing enter.

I wondering how to stop a endless loop.
The program clears a file then opens it and refreshes it over and over(the file is going to be modified by another program).
I want it to start from the beginning anytime i press enter or escape, it doesn't really matter as long as you can restart it.

Here is the complete code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <cstdlib>
#include <iostream>
#include <fstream>
#include <conio.h>
#include <windows.h>


using namespace std;

void x() {Sleep(1000);}

ofstream mynotes;
string output;

int main(void) 
{
    //It needs to start over from here
    mynotes.open("example.txt");
    mynotes << "";
    mynotes.close();
      
    //Here starts the endless loop    
    for(;;)
    {
        ifstream notes("example.txt");      
        if (notes.is_open()) 
        {
            while ( getline (notes,output) )
            {
                cout << output << "\n";
            }
            cout << "";
        }
        notes.close();
        cout << "Press enter to start over.";
        x();
        system("CLS");
    }
}
I don't think there is a simple platform independent way of doing this. You will have to try a library like curses, or maybe use the signals mechanism available on linux.
I didn't even know that you can get our of an endless loop in any way at all
Topic archived. No new replies allowed.