Alternative to system("Pause");

Jul 20, 2012 at 4:26pm
I spent about and hour reading past topics in the forum, and yes I know this is a sore point. So as a beginner, I would like to get it right from the start. I understand the dangers of using System() calls, so I'm trying to write an alternative to sysytem("pause"). This is the code I've written so far, but how compatible is it with other compilers or systems?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//Tested OK on DevC++ 4.9.9.2, Borland C++ Builder 6.0 and MSVC++ 6.0

#include <conio.h>
#include <iostream>

using namespace std;

void pause()
{
     cout << "Press any key to continue....";
     while(1)
     {            
          if(kbhit())
          {
               break;
          }
     }
}     

int main()
{
     pause();
     return EXIT_SUCCESS;
}
Jul 20, 2012 at 4:44pm
char f;
cin >> f;

Works fine. Even if its ugly.
Otherwise i have no idea
Jul 20, 2012 at 4:45pm
I have been told so many times that kbhit() is "not standard C++" but I'm not sure what that matters as far as compatibility with most used architectures (linux,win,mac). The only thing I'd suggest is you call getch() before breaking out to clear the key that was hit out of the buffer.
Jul 20, 2012 at 7:27pm
I have been told so many times that kbhit() is "not standard C++"


A keyboard is not standard C++ :p
Jul 23, 2012 at 9:29am
@Moschops
I have been told so many times that kbhit() is "not standard C++"


Thanks for clearing that up.
Jul 23, 2012 at 10:08am
try using cin.ignore().
Jul 23, 2012 at 10:14am
Here's a fair solution.
cin.get();
Jul 23, 2012 at 11:22am
What about system("CLS");
Jul 23, 2012 at 11:28am
Jul 23, 2012 at 11:50am
Oops! Not the old system("CLS"); again LOL
Topic archived. No new replies allowed.