Screen wont stay open

No errors found but whenever i compile it. the screens just opens and close suddenly.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <fstream>
#include <iostream>

using namespace std;

int main()
{
string line;    
ifstream a_file ( "example.txt" );

if ( !a_file.is_open() ) {
  // The file could not be opened
}
else {
     getline (a_file,line);
     cout<<line<<endl;
     }
     a_file.close();
     
     
     return 0;
     }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <fstream>
#include <iostream>

using namespace std;
#include <conio.h>
int main()
{
string line;
ifstream a_file ( "example.txt" );

if ( !a_file.is_open() ) {
  // The file could not be opened
}
else {
     getline (a_file,line);
     cout<<line<<endl;
     }
     a_file.close();
while(_kbhit()) _getch(); //first clear all key events from the console input buffer
     _getch(); //wait for the user to press a key
     return 0;
     }

     }
Ugh I wouldn't really suggest using conio to keep open the console as it is deprecated.
http://www.cplusplus.com/forum/beginner/1988/
Topic archived. No new replies allowed.