>#include If your compiler supports getch, it will most likely be in conio.h. But because getch is not a standard function, cin.get() is recommended instead:
#include <iostream>
using namespace std;
// Any other headers you need
int main()
{
// Your program here
cin.get();
} This will work except in cases where cin>> leaves a newline in the stream. This occurs more often than you might think, so it would be a good idea to flush the stream first and then call get:
#include <iostream>
#include <limits>
using namespace std;
// Any other headers you need
int main()
{
// Your program here
cin.ignore ( numeric_limits<streamsize>::max(), '\n' );
cin.get();
} There are other ways to flush the input stream, most of them are nonstandard. One nifty standard way is to use rdbuf:
cin.ignore ( cin.rdbuf()->in_avail() ); Though whether that's better than numeric_limits is debatable. And there's always the brute force loop:
char c;
while ( cin.get ( c ) && c != '\n' )
; Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401