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. Git与TortoiseGit使用方法

               下载这两个工具 Git地址:https://git-for-windows.github.io/ TortoiseGit地址:http://tortoisegit.org/ 点击 ...

  2. 百度开源分布式id生成器uid-generator源码剖析

    百度uid-generator源码 https://github.com/baidu/uid-generator snowflake算法 uid-generator是基于Twitter开源的snowf ...

  3. static变量、static方法之间的异同

        private SchemeBean getEmptyScheme() {        SchemeBean scheme = new SchemeBean();        scheme ...

  4. C# Programming Guide-->Statements, Expressions, and Operators-->Anonymous Functions

    C# Programming Guide Anonymous Functions Lambda Expressions Anonymous Methods In versions of C# befo ...

  5. Windows 下 ORA-12560: TNS: 协议适配器错误的问题

    Windows 下 ORA-12560: TNS: 协议适配器错误的问题原因有三个: 1.监听服务没有起起来.windows平台个一如下操作:开始---程序---管理工具---服务,打开服务面板,启动 ...

  6. UVA 1213 Sum of Different Primes(经典dp)

    题意:选择k(k<15)个唯一质数,求出和为n(n<1121)的可能数 题解:预处理dp,dp[k][n]表示使用k个素数拼成n的总方案数 就是三重枚举,枚举k,枚举n,枚举小于n的素数 ...

  7. 跟着vamei复习概率论

    最近重新看了一下概率论,感觉很多东西都遗忘了,还会陷入各种误区,赶紧的纠正回来. 概率论这块,主要内容包括: 事件.条件概率.随机变量.随机变量的分布函数.概率密度.联合分布.期望.方差.协方差. 我 ...

  8. No input file specified. nginx服务器报错解决

    配置虚拟域名的时候报No input file specified. 仔细观察一圈也没有发现任何错误,最终,我把注意力放到 \ 上.才发现问题. 以前我都是直接copy文件路径过去 文件里面 都是使用 ...

  9. js 弹出层,以及在javascript里定义层样式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. IDEA.环境

    1.下载: https://www.jetbrains.com/idea/download/#section=windows 安装的版本是:Ultimate:ideaIU-2017.3.3.exe 2 ...