You need to import system("pause"); to use stdlib.h. If it still doesn’t work, it is unreasonable and may have something to do with your machine environment
When using scanf after getchar(), you need to clear the input buffer first, because when scanf is entered and the carriage return is entered, scanf is triggered to receive the previous data, but the 回车 character is still in the input buffer, getchar()This character will be obtained directly, so the window cannot be blocked from closing
The solution is
First fflush(stdin) (refresh the input buffer), then getchar()
Twicegetchar(), the first time will swallow the previous 回车, and the second time will block the program execution, waiting for input
You need to import
system("pause");
to usestdlib.h
. If it still doesn’t work, it is unreasonable and may have something to do with your machine environmentWhen using
scanf
aftergetchar()
, you need to clear the input buffer first, because when scanf is entered and the carriage return is entered, scanf is triggered to receive the previous data, but the回车
character is still in the input buffer,getchar()
This character will be obtained directly, so the window cannot be blocked from closingThe solution is
fflush(stdin)
(refresh the input buffer), thengetchar()
getchar()
, the first time will swallow the previous回车
, and the second time will block the program execution, waiting for inputAdd the last line system("pause"); to include the header file #include <stdlib.h>