In a c++ program, if it is a window, sometimes it will disappear with a flash. If you don’t want it to disappear, add:
system(“pause”);
Note: Do not add after the return statement, it will not be executed.
Analysis:
System () is a call to the system command;
pause pause command;
When run here, it will say “Press any key to continue…” or “Press any key to continue…” ;
In VS2008, can be called directly
VC 6.0, to add the following header file!
#include < stdlib.h>
The
Supplement: http://bbs.csdn.net/topics/390231844
http://www.gidnetwork.com/b-61.html (the answer), 9/f,
1:
I don’t know why I often see people declare “void main” on CSDN
is not a standard entry point for C++
standard supports only two kinds of announcements
first type “int main”
int main(int argc, char *argv[]))
declares “void main” may have unexpected results
this doesn’t just apply to C++, C works as well
is this the textbook’s fault?Or is it the professors’ fault?
2:
do not use system(“pause”) to pause. Use STD ::cin. Get or getchar() instead.
why don’t you use system(“pause”)?
for two reasons
1: not portable
Two: it’s very expensive
where is it important?Let’s look at the process of system(“pause”)
1: pause your program
2: start the OS in the sub-process
3: finds the command to execute and allocates the memory for it
4: wait for input
5: recycle memory
6: end OS
7: continue your program
Getch () :
header file: conio. H
function purpose: read a character from the console, but not displayed on the screen
e.g. :
char ch; Or int ch;
getch (); Or ch = getch ();
with getch (); It waits for you to press any key before continuing with the following statement;
with ch = getch (); It waits for you to press any key, assigns the ASCII character to ch, and then executes the following statement.
getchar():
Extract characters from IO stream!
this function is declared in the stdio.h header file and used to include the stdio.h header file. Such as:
# include< stdio.h>
int getchar (void);
getch has the same basic functions as getchar, except that getch gets the key value directly from the keyboard and does not wait for the user to press enter. As soon as the user presses a key,
getch returns immediately. Getch returns the ASCII code entered by the user and returns -1 on error. The
getch function is commonly used in program debugging. During debugging, the relevant results are displayed in a critical position for viewing, and then the getch function is used to pause the program,
when any key is pressed after the program continues to run.
the first is that the two functions exist in different header files, this one basically you write #include< stdio.h> Getchar (), can accept a character, press enter to end, and display on the screen, and can clear forward just write
2. Getch (), receive a character, on the screen does not show
you write more, practice should be understood
Getchar () gets a character from the input device that is displayed on the screen, getch gets a character from the input device,
but the character is not displayed on the screen, for example:
#include < stdio.h>
int main()
{
printf(“%c”,getchar()); Suppose you get a character f from the keyboard here and press enter and you’ll see something like this
f
f
the first f is the f that you typed in, the second f is the f that printf gets
#include < stdio.h>
int main () {
printf (” % c “, getchar ());
}
suppose you enter an f and the result is
f this f is the printf output f
getchar is optimized,
getchar input character, until you press enter, then execute the code
getch without hitting enter
System (“pause”) can freeze the screen to observe the execution results of the program.
getch can not only pause the program
but also get a character
system(“pause”) is just a simple pause
the difference is the mechanism of action, although the effect looks the same. The
system return value is the result after you call the Shell command, and the getch() function will return the result provided by the function.
usually, the return value of Shell command may be unexpected and uncertain. Sometimes, it is impossible to judge whether the command
is executed successfully through the return value, which will have an impact on the program that conducts subsequent processing according to the return value. The return value of the function determines whether the
line is held successfully. But you don’t judge the return value at all, and you don’t process it, so you don’t have to worry about these differences.
Read More:
- C + + uses system (“pause”) to pause black windows
- use cin.get () instead of system (“pause”) to avoid the flash of C + + programs
- Usage of pause function in MATLAB
- STOP: c000021a { Fatal System Error } the initial session process or system process terminated …
- How to use C # to get image format without system. Drawing. Common
- Difference between vs code user and system version
- The solution to the black window flash of running program on Visual Studio
- Solve the audio control and report domexception: the play() request was interrupted by a call to pause in the chrome console
- Problems encountered in window10 system configuration tesseract
- Windows 7 system, start to run input CMD, the window flashed by, disappeared
- net start mysql System error 2 has occurred. The system cannot find the file specified.
- Debug assembler NTVDM has encountered a system error The equipment connected to the system does not work
- Solution to prompt “system group policy forbids installation of this device” in win10 system
- Error messages of copying files to Linux system by PSCP in Windows operating system
- Solution to msxml3.dll error ‘800c0008’ the system cannot locate the resource specified
- C#: Analysis of the difference between write() and writeline()
- [Solved] Imx6qdl Android 5.0 kernel 3.10 can’t mount system file system
- The solution of “failed to load ldlinux. C32” in Linux system
- Command prompt window prompt system error 5, access denied?
- Solution of command line window flashback when visual studio runs C + + program