It is currently Thu Jun 29, 2017 12:24 am




 Page 1 of 1 [ 2 posts ] 
Author Message
 Post subject: In case your console application quits automatically.
PostPosted: Mon Jun 24, 2013 5:48 pm 

Joined: Thu Jun 20, 2013 3:01 pm
Posts: 1
So when I was making a console app today, my app just closed after printing out it's text.
I fixed it by doing this:

1) adding #include <iostream> under #include "stdafx.h" (Not that it'll build succesfully without it)
2) adding this:
cin.clear();
cin.ignore(255, '\n');
cin.get();
right before return 0;

So in case of a 'Hello World' app, it would look like this:

#include "stdafx.h"
#include <iostream>
 
int main()
{
    using namespace std;
    cout << "Hello world!" << endl;
   cin.clear();
   cin.ignore(255, '\n');
   cin.get();
    return 0;
}

Not sure if I posted this in the right section or not, but maybe it'll help someone. (or at least I hope so)


Offline
 Profile  
 
 Post subject: Re: In case your console application quits automatically.
PostPosted: Mon Jun 24, 2013 6:17 pm 
Site Admin

Joined: Sun Feb 11, 2007 8:59 am
Posts: 1094
Location: Ontario Canada
Alternatively, if you don't want to add extra code, you can run your program by pressing Ctrl+F5. This will run your program but not close the console window when it is done. If you just press F5 in visual studio, your program will run in debug mode and when the execution is done, your console window will automatically close.


Offline
 Profile  
 
Display posts from previous:  Sort by  
 Page 1 of 1 [ 2 posts ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Jump to:  

cron