Join 307,102 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,017 people online right now. Registration is fast and FREE... Join Now!
Seems a little pointless, and it will only run on Windows.
All that you're achieving by putting "pause" at the end of that batch file is the equivalent to this: system ("pause");
Yes, you are right. A great deal of what I do seems pointless. Unfortunately I am obliged to work under Windows at the moment. (Some day the customer will want Vista, oh my.) Me, I dream in Unix. I do so miss vi.
Anyway, the point is, my batch file method works without changing the code. Sometimes I only have the exe and no source code.
I think I've created a platform independent method of using system commands like "cls", "clear", or "pause", but I really need someone with a linux machine to test this on. Of course credit goes to some of the solutions I've seen on here, and one of my classmates who helped me specifically with automating the "cls" or "clear" process. Here's the code seeing as I can't upload header files:
cpp
#include <stdlib.h> //For system commands
//Detect and define which command to use #ifndef WIN32 #define COMMAND "clear" //Clears a linux console screen #else #define COMMAND "cls" //Clears a windows console screen #endif
#define wipe() system( COMMAND ) //To clear the console screen use wipe();
#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);} //To pause the program or hold the console screen open use wait();
The commands should be pretty well commented, but just in case use:
wipe(); //This will clear the console screen wait(); //This will pause the program waiting for input i.e a enter key pressed
I will even make this as easy as I can for you. Here's a program you can test this with:
cpp
#include <stdio.h> #include <stdlib.h> //For system commands
//Detect and define which command to use #ifndef WIN32 #define COMMAND "clear" //Clears a linux console screen #else #define COMMAND "cls" //Clears a windows console screen #endif
#define wipe() system( COMMAND ) //To clear the console screen use wipe();
#define wait() {printf("Hit \"Enter\" to continue\n");fflush(stdin);getchar();fflush(stdin);} //To pause the program or hold the console screen open use wait();
int main (void) { printf("This is the console screen before we clear it.\n"); wait(); wipe(); printf("A new line in the console screen. Looks like we cleared the screen successfully"); wait(); }
Check that on your system for me and let me know how it works. Thanks.
This post has been edited by webmin: 17 Mar, 2009 - 08:25 PM
About the mac commands, I didn't really even think of them, nor do I know them (It's been about six years since I actually used a mac). If anyone would be willing to contribute those commands, I may be able to incorporate them.
As for the system commands, they are only used on on the clear, and cls commands, which was the simplest way I found to dealing with clearing the console screen. I do see what you mean by the system commands being a little taxing on the system trying to find the command, waiting for input, and then exiting the command. That's what makes system ("pause") so bad. However cls, and clear, while they work on the same principle they don't have to wait for user input to clear the console. Also the command works near instantaneous. They way I see it, if your computer has a problem taking on these commands, then you need a new machine to program on. And I can say that I at least tried to account for it being used on another system, of which the only one I didn't account for is a MAC.
If someone has a mac and would like to test this, go ahead, and then post the results here, or if you know the specific commands to clear your console screen, or pause the program, then by all means please share. This is going into a personal project of mine to create a header file that can perform these tasks with simplicity and ease. Please let me know if you have any tips or suggestions. Thanks
One way I've always paused the CMD prmt was simple. Inswert a dummy string or int and request it right before the return 0; or the last }, whichever method of C++ you use. Like so:
CODE
#include <string> int main() { //some code your doing here string QUIT; //now you could just as easily use char or int //string is just more fail safe. std::cin >> QUIT; return 0; }
not the best but would be platform good no matter where just don't forget to always #include <string>.
This post has been edited by Fussion289: 20 Mar, 2009 - 12:13 PM
The methods posted here don't seem to work if you have multiple exit points like,
CODE
#include <iostream> #include <string>
#include "DummyClass.h"
using namespace std;
int main ( int argc, char* argv[] ) { Dummy A_Dummy;
if (!A_Dummy.Do_Whatever_The_Dummy_should_Do()) return 1;
A_Dummy.PrintResult(); // Whatever you use to keep the console open here return 0; }
Your code will only catch the default exit and not the much more interesting error messages.
So i found this to be useful, might be some fault in it but it is standard and seems to be working:
CODE
#include <iostream> #include <string>
#include "DummyClass.h"
using namespace std; void KeepConsoleWindowOpen() { //Whatever code you use for that here } int main ( int argc, char* argv[] ) { atexit(KeepConsoleWindowOpen); Dummy A_Dummy;
if (!A_Dummy.Do_Whatever_The_Dummy_should_Do()) return 1;
A_Dummy.PrintResult(); // Whatever you use to keep the console open here return 0; }