Which is best way to pause the console in C++ programs?

  1. using cin.get()
  2. or using system("pause")
  3. or using C functions like getch() or getchar()?

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:

    1. suspend your program

    2. call the operating system

    3. open an operating system shell (relaunches the O/S in a sub-process)

    4. the O/S must now find the PAUSE command

    5. allocate the memory to execute the command

    6. execute the command and wait for a keystroke

    7. deallocate the memory

    8. exit the OS

    9. 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的更多相关文章

  1. c++中system("pause")的作用和含义

    简单来说就是暂停的意思,一般在LINUX编程时会用到,等待接收信号,才会重新运行 . 在进行C/C++编程的时候,在运行程序查看输出效果时,会出现窗口闪一下就关闭的情况. 在C语言中一般通过添加get ...

  2. 枚举系统磁盘驱动器(使用GetLogicalDriveStrings API函数,system("pause"); 很实用,还用到wcslen等函数)

    代码如下: #include "stdafx.h" #include <vector> #include <string> #include <Win ...

  3. 解决C/C++程序执行一闪而过的方法(使用getchar,或者cin.get,不推荐system(“pause”))

    简述 在VS编写控制台程序的时候,包括使用其他IDE(Visual C++)编写C/C++程序,经常会看到程序的执行结果一闪而过,要解决这个问题,可以在代码的最后加上system(“pause”).g ...

  4. ubuntu下vscode认识 system("pause")的解决办法

    linux下运行c++程序时,希望控制台不会输出后马上消失. 在windows系统下,用如下语句: #include <cstdlib> system("pause") ...

  5. while(1)和system("pause")区别

    我们在调试时,有时候会用到这两个语句. 1.显而易见,第一个是一个循环函数,占cpu.占内存: 2.system("pause")是一个系统调用,占内存,不占cpu;这个开销还是有 ...

  6. notepad++编译并运行java (自定义包)

    最近用Notepad++写汇编,感觉用起来挺顺手,于是想能不能也在这个优秀的编辑器下编写java并编译运行呢,因为每次启动eclipse都要挺长时间,而且eclipse实在太占内存了... 于是各种百 ...

  7. 20160129.CCPP体系详解(0008天)

    程序片段(01):函数.c+call.c+测试.cpp 内容概要:函数 ///函数.c #include <stdio.h> #include <stdlib.h> //01. ...

  8. c++ primer( 文本查询程序)

    读取用户指定的任意文本文件,然后允许用户从该文件查找单词,查询的结果是该单词出现的次数,并列出每次出现所在的行,如果某单词在同一行中多次出现,程序将只显示改行的一次.行号按升序显示(int main( ...

  9. jrtplib使用注意事项

    一.说明 RTP 现在的问题是要解决的流媒体的实时传输的问题的最佳方法.和JRTPLIB 是一个用C++语言实现的RTP库.包含UDP通讯.刚使用JRTPLIB,对JRTPLIB的理解还不够深,当做使 ...

随机推荐

  1. Python学习笔记之Python的多重继承和MixIn

    多重继承 继承是面向对象编程的一个重要的方式,因为通过继承,子类就可以扩展父类的功能. 回忆一下Animal类层次的设计,假设我们要实现以下4种动物: Dog - 狗狗: Bat - 蝙蝠: Parr ...

  2. Apache 部署HTTPS

    Apache 部署HTTPS 系统:Linux Centos 7.4 x64 应用:Apache 2.4.6 需要安装:mod_ssl 注:确认开启 Include conf/extra/httpd- ...

  3. JAVA-JVM垃圾回收算法

    哪些对象可以回收,有两种算法: 1. 引用计数算法,对象被引用计数器加1,对象被释放计数器减1.计数器为0的对象是可以被回收的. 此种方法优点:简单.缺点:会存在互相引用的两个对象,但实际这两个对象都 ...

  4. golang test 单元测试

    golang自家的单元测试做的很好了,自需要"文件名_test.go" 就可以在里面写单元测试,而且go test命令也很强大,可以只运行单个测试函数,在goland 可以点击单元 ...

  5. RocEDU.阅读.写作《苏菲的世界》书摘(四)

    亚理斯多德认为,快乐有三种形式.一种是过着享乐的生活,一种是做一个自由而负责的公民,另一种则是做一个思想家与哲学家.接着,他强调,人要同时达到这三个标准才能找到幸福与满足. 亚理斯多德提倡所谓的&qu ...

  6. Mac开发配置手册

    系统设置 在任何的操作系统中,首先你需要做一件事就是更新系统,点击窗口左上角的  > 关于本机 > 软件更新.此外,如果这是一部新的电脑,你还需要到系统设置进行一些适当调整.如何调整,取 ...

  7. unix_timestamp() 和 from_unixtime()

    unix_timestamp() 将时间转换为时间戳.(date 类型数据转换成 timestamp 形式整数) select unix_timestamp('2016-03-23 11:10:10' ...

  8. Flume实例一学习

    cp conf/flume-env.sh.template conf/flume-env.sh 打开flume-env.sh,配置Java环境变量 [root@test1 apache-flume-- ...

  9. jQuery全局冲突案例,解决$.noConflict()

    如图:犹豫$在js中可以作为一个变量去定义,所以在引入jQuery包之前定义了$对象,那么,在引入jQuery包之后就不能使用$对象了 解决:使用$.noConflict()她可以返回一个对象,这个对 ...

  10. MySQL索引最左原则

    通过实例理解单列索引.多列索引以及最左前缀原则 实例:现在我们想查出满足以下条件的用户id: 因为我们不想扫描整表,故考虑用索引. 单列索引: ALTER TABLE people ADD INDEX ...