tags:

views:

400

answers:

10

In C, I normally use the getch() function to wait for a key to be pressed and then end the program, but recently I read that since it's not a standard function, it's bad programming practice to use it.

So instead of the following:

int main() {
    dosomething();
    getch(); //wait for the user to see the results and press a key to end
    return 0;
}

What should I do to replace the getch ?

+2  A: 

You could fix the terminal window to not go away.

ndim
@ndim: how do you do that for Windows?
Lazer
Last time I looked, there was some setting to that effect in some menu.
ndim
+1  A: 

getchar() is standard but due to line buffering you will still need to press Enter before getchar returns.

Robert Gamble
A: 

Are you using Turbo C compiler? getch() is widely used there.

OTOH if you want the program to wait, run the binary inside the shell/terminal/prompt. Navigate to that directory and invoke

Windows:

C:> executable.exe

Linux:

~$ ./exec

You can use getchar(), the only problem being that it is buffered. getch() isnt buffered. You can make getchar() also to be non-buffered, but the code for that is not worth it. I tried it once, but due to it's sheer complexity never bothered to try it again.

Manish Sinha
+1  A: 

I prefer running program from command line.

Burgos
no use if you are running in the debugger as the output disappears. (Personally, I set a breakpoint...)
Stuart
With `Code::Blocks`, the terminal window keeps showing after the program has been executed: http://www.codeblocks.org/
Andreas Grech
+7  A: 

getc(stdin); is portable in this sense

AlexKR
...but leaves only one key that may be pressed with the same effect as when using getch().
Andrew Y
I think that's what the OP wants.An efficient way (in case you want to have a lot of user response) would be: #define GL puts("Press any key to continue..."); getc(stdin); calling GL; from your code.
Shimmy
BTW it doesn't seem to work in C++, I tried getc(stdin) and it doesn't continue till I press Enter.
Shimmy
A: 

In addition to other answers, you can also use this in windows, especially if your intention is to wait for key press to finish the program:

system("pause");
What's standard in `system("pause");`? Andreas wants to make his program more standard, not more dependent on a specific OS
pmg
I clearly stated that my answer was for Windows. I also said that I bring this option as and addition and what might be good use case for it. The issue with getch is not OS specific, it's compiler specific. Person shouldn't be downvoted for bringing another perspective to the issue.
+1  A: 

I think using getch() is the most common by far is keeping a console window from closing , but in C++ most experienced programmers will recommend that you use cin.get

 std::cin.get();

instead of:

getch();
Wael Dalloul
A: 

Just trying to answer something different, you can use

while(1) sleep(10);

at the end of main function (before return 0;) and then you can press ctrl-c to terminate the program. I think you should not face portability issue and I hope this is not bad programming practice either :)

vinit dhatrak
@Andreas Grech what do you think which one is best suited answer to you ?
vinit dhatrak
+3  A: 

Using a non-standard I/O function isn't bad practice per se; it becomes an issue if you intend to port this code to a different platform that doesn't support getch().

The problem is, there are no standard library functions that detect individual keystrokes (getchar() and fgetc() are buffered and require you to hit Enter before they return); you're going to have to use something non-standard to do that. If you only intend for this code to run on this specific platform, use getch() if that's what you need and just remember that this solution won't necessarily apply to other platforms. If you intend to port this code to platforms that don't support getch(), isolate the system-specific call behind a wrapper function so that when you port the code, only the code inside the wrapper function needs to change.

int getKeystroke(void)
{
  /**
   * Put platform-specific call here
   */
  return getch();
}

int main(void)
{
  ... // do stuff here
  getKeystroke();
  return 0;
}
John Bode
A: 

hello we have an 'exe' program. to run it repeatedly, we need to use some buttons of keyboard repeatedly. we need a batch file to run this program and represent the buttons of keyboard automaticly. for example, to run the '1.exe' program repeatedly, we must press the 'ENTER' & 'Esc' & ... Buttons of keyboard repeatedly. Then we have a program that need a batch file: "system(1.BAT) we appreciation your concern.

RBM