Stop infinite loop by pressing enter.

I wondering how to stop a infinite 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");
    }
}
To stop a loop you hit the END OF FILE button or

If the condition type was int do a double value
http://www.cplusplus.com/forum/beginner/27441/#msg146817

If a key is pressed, read a single character from the input. (You'll have to have unbuffered input for that.) If it is the Enter or Esc key, then break your loop. Otherwise continue.

Good luck!
Topic archived. No new replies allowed.