Join 307,143 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,746 people online right now. Registration is fast and FREE... Join Now!
The below code will "pause" the console. Plus, it's ANSI C, so it'll work in C/C++
CODE
#include <stdio.h>
int main(void) { printf("Hit \"Enter\" to continue\n"); fflush(stdin); /* Remove anything that is currently in the standard input */ getchar(); /* Wait for the user to hit the enter key */ fflush(stdin); /* Remove any additional charcters the user may have inputed */
return 0; }
Of course, this is a lot more useful as a macro
CODE
#include <stdio.h>
#define PAUSE {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);}
int main(void) { PAUSE
return 0; }
PAUSE contains it's own scope, so it will not produce adverse effects. IE, this will work
CODE
if(foo) PAUSE;
Now, I personally don't like to have to key a extra key to quit, but I love the macro for debugging, as long as you remove the printf(). For example, useing a non recursive string reversal.
void ReverseString(char *str) { int len = strlen(str) - 1; int i; char c;
for(i = 0; i < len; ++i, --len) { c = str[i]; str[i] = str[len]; str[len] = c;
/* Note: These are offset to the left to make it apparent they are debug data */ { int j; printf("%s\n", str); for(j = 0; j < i; ++j) putchar(' '); putchar('^'); for(; j < len - 1; ++j) putchar(' '); putchar('^'); PAUSE; putchar('\n'); } } }
Edit: Forgot a fflush(stdin);
This post has been edited by Cerolobo: 9 Apr, 2008 - 03:47 AM
I think my point may have been missed. I advise against the use of getch(). It is a platform dependent function that does not comply to ANSI standards. It is not part of standard C++.
Are developers who don't know simple things, like how to stop the debugger from closing the console window after execution has stopped, really interested in porting code to multiple platforms?
Better than any of these recommendations, in my opinion, is to learn how to use the debugger. If someone wants to view the output before the application closes, then they should place a breakpoint in the code before the application closes. Debugging is an important skill. You can't start too early!
I think my point may have been missed. I advise against the use of getch(). It is a platform dependent function that does not comply to ANSI standards. It is not part of standard C++.
Are developers who don't know simple things, like how to stop the debugger from closing the console window after execution has stopped, really interested in porting code to multiple platforms?
Better than any of these recommendations, in my opinion, is to learn how to use the debugger. If someone wants to view the output before the application closes, then they should place a breakpoint in the code before the application closes. Debugging is an important skill. You can't start too early!
An excellent point...but I fully expect that well over 95% of the users of this board not to use a debugger. Further, i usually try to endorse proper debugging through the use of old school debugging techniques - the manual insertion of specific breakpoints in the code.
If you review many of the topics on the board that ask about holding the execution window open, most are not trying to track bugs...they are only trying to see the end result, but have only ever learned how to program using an IDE and do not understand how that IDE works. Despite the fact that I have grave reservations about programmers who ONLY know how to use an IDE, I try to at least provide them with some basic understanding of that IDE's functionality.
Frankly, if I had the time to explain how to use the debugger (or debug mode) of whatever IDE/compiler they are using, I'd point them towards the documentation for that specific IDE/complier and use the time to have a beer.
I'll be honest with you, I've never actually seen that before.
However, I would still advise against the use of it, since conio.h does not conform to all standards, and it isn't going to be around forever. I always see a lot of people using conio.h in their programs~ granted, it has some very useful functions, but it's best to stick to the standards.
Personally, I tend to use cin.get (); (in C++ anyway) Granted, it waits for the user to press enter, but we could just prompt them:
cpp
#include <iostream>
void pause () { std::cout << "\nPress the enter key to continue . . ."; std::cin.get (); }
Thank you for the idea. I will ask them, especially when now I've just changed all the code in this big program I am making to "cin.get();" instead of "while(!_kbhit());" and it works perfectly! lol.