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的理解还不够深,当做使 ...
随机推荐
- cogs 547:[HAOI2011] 防线修建
★★★☆ 输入文件:defense.in 输出文件:defense.out 简单对比 时间限制:1 s 内存限制:128 MB 题目描述: 近来A国和B国的矛盾激化,为了预防不测,A国 ...
- ContOs 将SpringBoot的jar制作成系统服务
将jar包上传到 你想要的目录下 如 /java/service/demo.jar 进入 /etc/systemd/system 目录下 新建 demo.service 内容如下 -------- ...
- 爬虫之动态HTML处理(Selenium与PhantomJS )动态页面模拟点击
动态页面模拟点击 #!/usr/bin/env python # -*- coding:utf-8 -*- # python的测试模块 import unittest from selenium im ...
- /nagios/cgi-bin/cmd.cgi无法打开
原因分析,nginx不支持post. 解决方法,重新编译nagios 1.vi /nagios-4.0.8/cgi/cmd.c 找到printf("<form method='post ...
- java中函数传值与引用问题
从C++转java,在使用函数传对象时,碰到一点问题,今天特意验证了一下: public class App { public static void doubleTest(double d) { d ...
- SQLite内存数据库操作类
using System; using System.Collections; using System.Collections.Generic; using System.Data; using S ...
- Vuex最基本样例
通过vue-cli建立基本脚手架(需要安装vuex),需要新建一个store.js文件.基本目录如下 1,store.js文件代码: import Vue from 'vue' import Vuex ...
- css 固定宽度,自动换行
max-width: 200px; display: block; word-break: break-all:
- 分享几道Java线程面试题
不管你是新程序员还是老手,你一定在面试中遇到过有关线程的问题.Java语言一个重要的特点就是内置了对并发的支持,让Java大受企业和程序员的欢迎.大多数待遇丰厚的Java开发职位都要求开发者精通多线程 ...
- PHP5.6版本安装redis扩展
一.php安装redis扩展 1.使用phpinfo()函数查看PHP的版本信息,这会决定扩展文件版本 2.根据PHP版本号,编译器版本号和CPU架构, 选择php_redis-2.2 ...