system(“pause”); - Why is it wrong?

前端 未结 13 2070
轻奢々
轻奢々 2020-11-21 07:39

Here\'s a question that I don\'t quite understand:

The command, system(\"pause\"); is taught to new programmers as a way to pause a program and wait for

13条回答
  •  盖世英雄少女心
    2020-11-21 07:49

    You can use std::cin.get() from iostream:

    #include  // std::cout, std::cin
    using namespace std;
    
    int main() {
       do {
         cout << '\n' << "Press the Enter key to continue.";
       } while (cin.get() != '\n');
    
       return 0;
    }
    

    Besides, system('pause') is slow, and includes a file you probably don't need: stdlib.h. It is platform-dependent, and actually calls up a 'virtual' OS.

提交回复
热议问题