Pause Console in C++ Program

How to pause in C?

you can put

getchar();

before the return from the main function. That will wait for a character input before exiting the program.

Alternatively you could run your program from a command line and the output would be visible.

How to pause the C program?

You can do it like that:

#include <stdio.h>
int main(){
puts("Press any key to continue ... ");
getchar();
}

If you really need to make it enter-accepted:

#include <stdio.h>
int main(){
puts("Press enter to continue ... ");
while(getchar()!=27);
}

27 is enter keycode.

Another C solution would be:

system("read -n1 -r -p \"Press any key to continue...\" key")

In c++ it would look like that:

#include <iostream>
int main(){
std::cout << "Press enter to continue ... " << std::endl;
std::cin.ignore(INT_MAX);
cin.get();
}

Pause Console in C++ program

There might be a best way (like using the portable cin.get()), but a good way doesn't exist. A program that has done its job should quit and give its resources back to the computer.

And yes, any usage of system() leads to unportable code, as the parameter is passed to the shell that owns your process.

Having pausing-code in your source code sooner or later causes hassles:

  • someone forgets to delete the pausing code before checking in

    • now all working mates have to wonder why the app does not close anymore
    • version history is tainted
  • #define is hell
  • it's annoying to anyone who runs your code from the console
  • it's very, very, very annoying when trying to start and end your program from within a script; quadly annoying if your program is part of a pipeline in the shell, because if the program does not end, the shell script or pipeline won't, too

Instead, explore your IDE. It probably has an option not to close the console window after running. If not, it's a great justification to you as a developer worth her/his money to always have a console window open nearby.

Alternatively, you can make this a program option, but I personally have never seen a program with an option --keep-alive-when-dead.

Moral of the story: This is the user's problem, and not the program's problem. Don't taint your code.

how can I pause my C program when a page of the console has been printed?

First you need to define what a "page" is. Then you will know how many lines are available. Then when printing you stop to get input every X lines (where X is the number of lines per page) before continuing printing the next X lines.

Because reading input will block until the user presses the Enter key (normally) then it will seem like your program pauses.

Why pause() function prevents seeing console output in C?

Your terminal emulator doesn't apparently flush standard output on every newline. You can manually flush it with fflush(stdout) if you want to see the output immediately:

int main(){
printf("Start\n");
fflush(stdout);
pause();
printf("Finish\n");
}

Pause screen at program completion in C

To do this quick hack, the most common two options are:

/* Windows only */
#include <stdlib.h>

system("pause");

and

/* Cross platform */
#include <stdio.h>

printf("Press enter to continue...\n");
getchar();

I suggest the latter method, though the first method really triggers on "any" key while the bottom one only triggers on enter.



Related Topics



Leave a reply



Submit