Sign in to follow this  

A trivial problem in C

This topic is 4675 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I know, I could have searched for this on Google, but I really need a quick solution instead of scanning through results (I'm that lazy). I am trying to write a console program which waits for the user to press a key after it's done executing, before it closes. Now, I know I can include conio.h and use the getch() function, but this does not work with Linux. How else can I possibly implement the "Press any key to continue..." feature? Thanks.

Share this post


Link to post
Share on other sites
Wait, why is conio unavailable in linux?

The conio stuff -came- from the unix world, didn't it?

Share this post


Link to post
Share on other sites
There is no pause command in Linux.

How about. . .

#include <iostream>

void pause()
{
using namespace std;
cout << "Press enter to continue.";
cout.flush();
cin.sync();
cin.get();
cin.sync();
}

Share this post


Link to post
Share on other sites
Sadly, I'm restricted to C. I cannot use any C++ code.
conio.h is not in the ANSI standard library so I suppose that's why it's not available.

Share this post


Link to post
Share on other sites
Use getchar(); which is implemented in all C distributions.

Share this post


Link to post
Share on other sites

#include <stdio.h>

void pause(void)
{
fputs("Press enter to continue.", stdout);
fflush(stdout);
while(getchar()!='\n');
}

Share this post


Link to post
Share on other sites
Sign in to follow this