What is the difference between using system("pause") and cin.get(pause)?
Oliver told me never use system("pause").

#include<string>
#include<iostream>

int main(){
 using namespace std;

  string str = "My name is shridhar";
  cout<<"What is your name"<<str;

  char pause = 0;
  cout << "Press enter to continue...";
  cin.get(pause);
}

Dani AI

Generated

As hinted, the two approaches are not just stylistically different — they have different semantics and consequences. Calling system("pause") invokes the C runtime system() to run an external command (the Windows pause built-in), which is non‑portable, relatively slow, and can be unsafe if the command string is constructed from untrusted input. Using the C++ input API (std::cin.get, std::getline, std::cin.ignore, etc.) keeps the program inside the runtime, is portable, and gives deterministic behavior.

Common pitfalls that aren't always mentioned in short replies:

  • Prompts must be flushed before waiting, otherwise the message may not appear. Use std::flush or std::endl.
  • If formatted input (operator>>) was used earlier, a leftover newline can make a single std::cin.get() or std::getline() return immediately. Use std::cin.ignore(...) to discard the rest of the current line first.
  • If the program is run non-interactively (input redirected or piped), std::cin may already be at EOF; code should handle that instead of hanging forever.
  • In IDEs, the console can be closed by the environment; configuring the IDE to keep the console open is a better long‑term fix than sprinkling system("pause") in code.

A simple, robust pattern to wait for Enter (portable C++):

#include <iostream>
#include <limits>
#include <string>

std::cout << "Hit ENTER to exit..." << std::flush;
// clear any leftover input (e.g. after operator>>)
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
// wait for the next Enter (handles empty or long lines safely)
std::string line;
if (!std::getline(std::cin, line)) {
    // input closed (EOF) or error — handle or exit gracefully
}

Conclusion: prefer standard C++ input techniques for portability and safety; system("pause") is a quick hack that is best avoided in shared or production code.

One (system("pause")) involves asking the operating to run a system command and spawn a new process and the other simply waits for input to your program. They are about as different as night and day. As to which to use, cin.get() is usually preferred. If you do a quick Daniweb search on system("pause"), the reasons have been written up pretty well.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.