C++第1天:在C和C++里,要尽量避免使用 system("pause")
2016-11-28 18:49
459 查看
版权声明:转载时请以超链接形式标明文章原始出处和作者信息及本声明
http://amigniox.blogbus.com/logs/47563613.html
I've never understood why system("PAUSE") isso
popular. Sure it will pause aprogram before it exits. This pause is very useful when yourIDE won't
waitas you test a program and as soon as the program finished thewindow closes taking all your data with it.
But using system("PAUSE") islike
burning your furniture for heat when you have a perfectly goodthermostat on the wall.
Many people, instructors included, for some inexplicable reasonthink that making a call to the operatingsystem and running a system command totemporarily
halt a program is a good thing. Where they get thisidea is beyond me. Reasons:
It's not portable. This works only onsystems that havethe PAUSE commandat
the system level, like DOS or Windows. But notLinux and most others...
It's a very expensive and resource heavy function call.
It's like using a bulldozerto open your front door. It works, but the key is cleaner, easier,cheaper. What system() doesis:
suspend your program
call the operating system
open an operating system shell (relaunches the O/S in asub-process)
the O/S must now find the PAUSE command
allocate the memory to execute the command
execute the command and wait for a keystroke
deallocate the memory
exit the OS
resume your program
There are much cleaner ways included in the language itself thatmake all this unnessesary.
You must include a header you probably don'tneed: stdlib.h or cstdlib
It's a bad habit you'll have to break eventually anyway.
Instead, use the functions that are defined natively in C/C++already. So what is it you're trying to do? Wait for a key to bepressed? Fine -- that's called input.So
in C, usegetchar() instead.In C++,
how about cin.get()?All you have to
do is press RETURNandyour program continues.
Note: the origin of the article isn't specified.
++
http://amigniox.blogbus.com/logs/47563613.html
system("pause")
I've never understood why system("PAUSE") issopopular. Sure it will pause aprogram before it exits. This pause is very useful when yourIDE won't
waitas you test a program and as soon as the program finished thewindow closes taking all your data with it.
But using system("PAUSE") islike
burning your furniture for heat when you have a perfectly goodthermostat on the wall.
Many people, instructors included, for some inexplicable reasonthink that making a call to the operatingsystem and running a system command totemporarily
halt a program is a good thing. Where they get thisidea is beyond me. Reasons:
It's not portable. This works only onsystems that havethe PAUSE commandat
the system level, like DOS or Windows. But notLinux and most others...
It's a very expensive and resource heavy function call.
It's like using a bulldozerto open your front door. It works, but the key is cleaner, easier,cheaper. What system() doesis:
suspend your program
call the operating system
open an operating system shell (relaunches the O/S in asub-process)
the O/S must now find the PAUSE command
allocate the memory to execute the command
execute the command and wait for a keystroke
deallocate the memory
exit the OS
resume your program
There are much cleaner ways included in the language itself thatmake all this unnessesary.
You must include a header you probably don'tneed: stdlib.h or cstdlib
It's a bad habit you'll have to break eventually anyway.
Instead, use the functions that are defined natively in C/C++already. So what is it you're trying to do? Wait for a key to bepressed? Fine -- that's called input.So
in C, usegetchar() instead.In C++,
how about cin.get()?All you have to
do is press RETURNandyour program continues.
Note: the origin of the article isn't specified.
++
相关文章推荐
- 在C和C++里,要尽量避免使用 system("pause")
- 使用cin.get()而不是system("pause")来避免c++程序一闪而过
- C++使用system( “pause “);来暂停黑窗口
- 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))
- 为什么 C++ 中提倡尽量避免使用宏 #define(转)
- C++尽量避免使用指针
- 查看C/C++程序执行结果的方法------system("pause"),getchar(),cin.get()
- 解决C、C++程序执行结果一闪而过的方法---system("pause")、getchar()、cin.get()
- C++中的system("pause");
- system("pause")在使用freopen时失效
- VS下使用freopen重定向输入流至文件同时使用system("pause")暂停
- 解决C、C++程序执行结果一闪而过的方法---system("pause")、getchar()、cin.get()
- c++中的system("pause")的作用
- 解决C、C++程序执行结果一闪而过的方法---system("pause"
- system("pause")使用时要加头文件iostream.h
- [C++再学习系列] 避免使用宏
- Visual Assist X尽量避免在非英文目录下使用,否则可能由于文件路径不能识别而导致不能进行提示
- system("pause")
- 页面传值避免使用"ID"
- Visual Assist X尽量避免在非英文目录下使用,否则可能由于文件路径不能识别而导致不能进行提示