-
Pause function ???
I'm looking for a pause function to use at the end of a simple C++ program to pause the screen so the user can see the output before it closes. I know there is a simple function that will do this, but I can't remember/find it anymore. Anyone who knows a function that will pause the screen or wait till a key is pressed? Thanks!
-
Code:
// Pause for the specfied time
#include <time.h>
void pause(int secs)
{
long tm1,test;
time(&tm1);
test = tm1 + secs;
while(tm1 < test)
{
time(&tm1);
}
}
// pause till any key is hit
#include <conio.h>
#include <stdio.h>
void pause(void)
{
while(!_kbhit());
getch(); // clear the key from the stdin
}
// Wait untill a specific key is hit
#include <conio.h>
#include <stdio.h>
void pause(char key)
{
char ch;
begin:
while(!_kbhit());
ch = getch();
if(ch == key) { return; }
else
goto begin;
}
-
Clear buffer ???
How do I clear the buffer before I use this method of pausing? My output is not appearing until after I hit a key.
-
whats the line of code that gets the input?
-
code
I am using cin and cout to display some stuff... and then I call a pause function. Similiar to....
cin >> Name
cout << "Hello ";
cout << Name << "\n";
Pause()
Pause function is....
void Pause()
{
char keyHit;
cout << "Press Any Key to Continue\n";
// Pause until the user presses a key.
getch();
return;
}
It does pause the screen waiting for any character to be pressed, but the output...
Hello Sam
is not shown until the additional character is pressed.
I assume that this is because the buffer is not getting sent to the screen before hand. Maybe I'm wrong. ???
-
i can't remember off-hand what causes this but this should fix it if not let me know and we'll try something else.
try
cout.flush();
before you call pause() this should fix it.
-
when using cout you must use endl
like this:
cout << "Enter a number: " << endl;
endl flushes the output to the screen and forces the cursor to the next line. To avoid forcing it to the next line use flush instead
like this:
cout << "Enter a number: " << flush;
and to pause the screen long enough to see the screen use the getchar(); //requires stdin.h I believe waits for keyboard entry
Also you can run your program from the command line by first opening MS DOS and changing the current directory to the location of your executable or placing it the c:\windows\command\ folder or .... change the path to some folder where you keep your executables..
-
With Vis C++ can't you just
Code:
#include<windows.h>
//code here
Sleep(x)
-
Why not just use "system("pause");" Then when <enter> is pressed it goes away