How can I pause at the end of my Command Line (Basic) Scripts (Programs)? All I want to do is, at the end of my running application, have it pause and wait until any key is pressed before exiting so that any final lines of text on the screen will stayand be legible.
~Saint Squireen~
4 replies to this topic
Ad:
#2 Crossbones+ - Reputation: 3518
Posted 28 August 2012 - 07:19 PM
That's a fine way to do it. In fact, that's how it's usually done. Basically, use your language's idiom for reading a key from the standard input. That should be getchar(), getkey(), get(), read(), readln(), or something along those lines. Those will hang until the user inputs some stuff in, so you can precede it with some message such as "Press a key to exit". Or, if it's available, system("PAUSE") which will do the same thing (sorry, I don't do Basic so I wouldn't know the correct syntax).
Of course, if you want to avoid this, you can always run the script from the command-line itself (i.e. start the command-line interpreter, cd to your program and run it), this way the program will run "inside" the command-line interpreter and won't close the console once finished. It might be a bit harder to set up but it's generally the best way to do it, as waiting for user input to terminate the program isn't very script-friendly, especially for long-running scripts. You do have to deal with the CLI awkwardness but overall it works better except for throwaway programs (where just waiting for a keypress is faster to whip up and more convenient).
So instead of:
Does it make sense?
Of course, if you want to avoid this, you can always run the script from the command-line itself (i.e. start the command-line interpreter, cd to your program and run it), this way the program will run "inside" the command-line interpreter and won't close the console once finished. It might be a bit harder to set up but it's generally the best way to do it, as waiting for user input to terminate the program isn't very script-friendly, especially for long-running scripts. You do have to deal with the CLI awkwardness but overall it works better except for throwaway programs (where just waiting for a keypress is faster to whip up and more convenient).
So instead of:
You would get:Hello World
*poof*
Copyright Windows blablabla
>> cd C:\some\path\to\my\program\
>> program.exe
Hello World
>>
Does it make sense?
#3 Members - Reputation: 103
Posted 29 August 2012 - 11:49 AM
In C++, the simplest way to pause a win32 console program is to make a call to the system such as:
#include <iostream>
using namespace std;
int main()
{
cout << "Hey, I'm paused!" << endl;
//program pauses an user is prompted to hit any key to continue
system ("pause");
return o;
}
#include <iostream>
using namespace std;
int main()
{
cout << "Hey, I'm paused!" << endl;
//program pauses an user is prompted to hit any key to continue
system ("pause");
return o;
}
#5 Members - Reputation: 121
Posted 31 August 2012 - 06:13 PM
If you are using Visual Studio, run your code by pressing Ctrl+F5, rather than just F5. This will pause the console window when your program finishes rather than closing it
---
Free C++, OpenGL, and Game Development Video Tutorials @
www.marek-knows.com
Play my free games: Ghost Toast, Zing
Free C++, OpenGL, and Game Development Video Tutorials @
www.marek-knows.com
Play my free games: Ghost Toast, Zing






