about system (pause) in cpp
Which is best way to pause the console in C++ programs?
- using
cin.get() - or using
system("pause") - or using C functions like
getch()orgetchar()?
Is it true that use of system("pause") leads to non portable code and can't work in UNIX?
Is cin.get() is better to use to pause console?
在C和C++里,要尽量避免使用 system("pause")
system("pause")
I've never understood why system("PAUSE") is so popular. Sure it will pause a program before it exits. This pause is very useful when your IDE won't wait as you test a program and as soon as the program finished the window closes taking all your data with it.
But using system("PAUSE") is like burning your furniture for heat when you have a perfectly good thermostat on the wall.
Many people, instructors included, for some inexplicable reason think that making a call to the operating system and running a system command to temporarily halt a program is a good thing. Where they get this idea is beyond me. Reasons:
It's not portable. This works only on systems that have the PAUSE command at the system level, like DOS or Windows. But not Linux and most others...
It's a very expensive and resource heavy function call.
It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:
suspend your program
call the operating system
open an operating system shell (relaunches the O/S in a sub-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 that make all this unnessesary.
You must include a header you probably don't need: 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 be pressed? Fine -- that's called input. So in C, usegetchar() instead. In C++, how about cin.get()? All you have to do is press RETURNand your program continues.
Note: the origin of the article isn't specified.
++
about system (pause) in cpp的更多相关文章
- c++中system("pause")的作用和含义
简单来说就是暂停的意思,一般在LINUX编程时会用到,等待接收信号,才会重新运行 . 在进行C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加get ...
- 枚举系统磁盘驱动器(使用GetLogicalDriveStrings API函数,system("pause"); 很实用,还用到wcslen等函数)
代码如下: #include "stdafx.h" #include <vector> #include <string> #include <Win ...
- 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))
简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...
- ubuntu下vscode认识 system("pause")的解决办法
linux下运行c++程序时,希望控制台不会输出后马上消失. 在windows系统下,用如下语句: #include <cstdlib> system("pause") ...
- while(1)和system("pause")区别
我们在调试时,有时候会用到这两个语句. 1.显而易见,第一个是一个循环函数,占cpu.占内存: 2.system("pause")是一个系统调用,占内存,不占cpu;这个开销还是有 ...
- notepad++编译并运行java (自定义包)
最近用Notepad++写汇编,感觉用起来挺顺手,于是想能不能也在这个优秀的编辑器下编写java并编译运行呢,因为每次启动eclipse都要挺长时间,而且eclipse实在太占内存了... 于是各种百 ...
- 20160129.CCPP体系详解(0008天)
程序片段(01):函数.c+call.c+测试.cpp 内容概要:函数 ///函数.c #include <stdio.h> #include <stdlib.h> //01. ...
- c++ primer( 文本查询程序)
读取用户指定的任意文本文件,然后允许用户从该文件查找单词,查询的结果是该单词出现的次数,并列出每次出现所在的行,如果某单词在同一行中多次出现,程序将只显示改行的一次.行号按升序显示(int main( ...
- jrtplib使用注意事项
一.说明 RTP 现在的问题是要解决的流媒体的实时传输的问题的最佳方法.和JRTPLIB 是一个用C++语言实现的RTP库.包含UDP通讯.刚使用JRTPLIB,对JRTPLIB的理解还不够深,当做使 ...
随机推荐
- jquery的prev选择器无效
今天使用jquery操作dom 需要把当前元素的同级元素中前面带有属性a=1的元素筛选出来. 查看api, .prev() 获得匹配元素集合中每个元素紧邻的前一个同辈元素,由选择器筛选(可选). 看 ...
- 20145314郑凯杰《信息安全系统设计基础》第八周复习总结 Part A
20145314郑凯杰<信息安全系统设计基础>第八周复习总结 Part A 学习知识点内容总结 复习线索:http://group.cnblogs.com/topic/73069.html ...
- 20145314郑凯杰 《Java程序设计》第3周学习总结
20145314郑凯杰 <Java程序设计>第3周学习总结 所有代码均已托管 地址https://git.oschina.net/qiaokeli26/codes 按照下面程序结果中的代码 ...
- 在Linux终端管理文件你要知道的11个命令
LS - 列表文件 ls命令列出目录中的文件. 默认情况下,使用ls列出当前目录下的文件. 2 你也可以列出文件递归-也就是说,列出所有文件在当前目录中的目录-使用ls -R.LS还可以列出在其他目录 ...
- CNN笔记:通俗理解卷积神经网络【转】
本文转载自:https://blog.csdn.net/v_july_v/article/details/51812459 通俗理解卷积神经网络(cs231n与5月dl班课程笔记) 1 前言 2012 ...
- validateJarFile jar not loaded. See Servlet Spec 2.3, section 9.7.2. Offending class: javax/servlet/Servlet.class
项目环境 Maven.Tomcat7.0.27.jdk1.8.0_111 报这个错误的原因是项目中依赖 javax.servlet-api 包和Tomcat本身的包冲突了,Tomcat本身也有这个包 ...
- .net 获取当前电脑账户
string domainAndName = User.Identity.Name; ] { '\\' }, StringSplitOptions.RemoveEmptyEntries); strin ...
- jQuery编程规范与最佳实践(附带一些个人的笔记)
加载jQuery-Loading jQuery 1.坚持使用CDN来加载jQuery,这种别人服务器免费帮你托管文件的便宜干嘛不占呢.点击查看使用CDN的好处,点此查看一些主流的jQuery CDN地 ...
- Educational Codeforces Round 54 (Rated for Div. 2) DE
D 给出一个无向图,需要删去一些边,想知道最后能有多少个点到1的距离还是过去那么短 如果求一个最短路,然后从删边的角度看,看起来很难做,但是如果从零开始加边就会有做法,如同prim那样,先加入和1直接 ...
- webstorm拉取git代码
在webstorm中VCS → git → clone → url就是你的git代码地址,parent Directory(你要放到的目录),Directiory Name(起一个项目名称)