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的理解还不够深,当做使 ...
 
随机推荐
- Visual Studio 2015 初体验
			
据微软介绍每次发布的新版本,都承载着为开发者提供最高效的Visual Studio开发体验的使命.Visual Studio 2015亦延续了这一趋势,为开发者带来了进一步的生产力创新,包括调试和诊断 ...
 - char、varchar与text
			
总结自:https://www.cnblogs.com/mjbrian/p/6866263.html char: 定长,长度范围是0~255. 当长度不足255时,用空格来填充剩下的字符. ...
 - python windows打包
			
接触过的工具有pyinstaller,或者py2exe.感觉pyinstaller更简单易用. 真正将依赖的dll打包称一个安装包还需要借助windows打包工具 Inno Setup 或 NSIS ...
 - 20145216史婧瑶《Java程序设计》第三次实验报告
			
实验三 敏捷开发与XP实践 实验内容 使用git上传代码,两个人进行小组合作,队友下载代码并修改再重新上传. 实验步骤 一. 使用git上传代码 1.找到需要push的文件所在文件夹,右键点击Git ...
 - Java学习第二周学习笔记
			
20145307<Java程序设计>第二周学习总结 教材学习内容总结 Java语言中的很多基本语法都和C语言类似,以下Java中的基本语法 标识符 标识符是程序中自定义的一些名称. 由26 ...
 - 求生之路 Hammer World Editor打开后闪退解决办法
			
试过WinXp.Win7.Win10 都无法正常启动Hammer,搜索N多资料后发现如图修改 控制面板 -> 区域 -> 格式 -> 英语[美国] 即可正常启动了!!!
 - Jquery7 表单选择器
			
学习要点: 1.常规选择器 2.表单选择器 3.表单过滤器 表单作为 HTML 中一种特殊的元素,操作方法较为多样性和特殊性,开发者不但可以使用之前的常规选择器或过滤器,也可以使用 jQuery 为表 ...
 - Mixed Authentication in IIS7
			
Process for Mixed Authentication Configuration in IIS7 Integration Mode There're some breaking chang ...
 - win10家庭版的defender注册表关闭和开启
			
关闭方法: 打开“命令提示符(管理员)”,然后输入: reg add "HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows Defe ...
 - ubuntu系统samba服务的安装配置
			
安装 sudo apt-get install samba 配置 打开Samba配置文件: sudo gedit /etc/samba/smb.conf 在其最后添加: [share] path = ...