> the dos screen flashes real fats and goes away
Well, are you double-clicking on the DOS program, or running it in Command Prompt? If you are doing the first, DOS programs always do that unless you need to retreive some type of data, or freeze the screen. DOS applications are usually run inside a Prompt, but if not, Windows treats it like an application.
My best bet would be to either pause the screen using:
system("pause");
Or for a more recommended step, just use conio.h's function called getch():
#include <conio.h>
int main() {
getch(); // wait for key press
return 0;
}
How getch() works
The function is normally defined in . getch() works by halting the application until the user enters a keystroke. getch() then returns the ASCII value of the keystroke as an int. System-wide keystrokes are processed by the operating system before they are returned by getch().
When getch() receives one of these keystrokes, it returns 0 to the calling program. The subsequent call to getch() returns a value for the key. Entering the following keystrokes will also cause getch() to return 0 to the calling program and return the key value on the subsequent call to getch().
Why system() calls aren't safe
Using system("pause") is a bad habit.
By calling system(), you are invoking the default shell. The shell executes the command-line given to it which runs the 'pause.exe' program. So your simple c program is relying on two external programs for checking a single key press. Though, what if someone deleted or renamed 'pause.exe'? Better yet, what if someone tried to compile your program on unix, or another operating system...It wouldn't work.
There are always alternatives:
#include <stdio.h>
void cmdwait(void) {
int c;
do {
c = getchar();
if (c == EOF)
break;
} while (c != '\n');
}
int main() {
// press any key to continue
cmdwait();
return 0;
}
Instead of calling on system("pause"); just call on cmdwait(); It's alot safer.
I hope this helps,
-Stack Overflow