what is the linux command counter part of system("pause")?

I wanted my program to pause the screen. I need it to work in linux and windows. I wished to use the system("xxx") command but dont know what system command is to be used in linux.

I just learned from the other post that system("cls") is for windows while system("clear") is for linux. So if system("pause") is for windows, what is for linux?

I know that cin.get works but I wished to use the system command if there is one.
There isn't. The most portable way to do it is a function like:
1
2
3
4
5
void pause()
{
  cout << "Press ENTER to continue... ";
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}


Hope this helps.
Duoas, consider the following. Your version does not work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <string>
using namespace std;

#define PAUSE 1    // 1 for pause1(), 2 (or whatever) for pause2()

void pause1()
{
    cout << "Press ENTER to continue... ";
    cin.ignore( numeric_limits<streamsize>::max(), '\n' );
}

void pause2()
{
    cout << "Press Enter to continue... ";
    cin.clear();  // clear any errors in the stream
    cin.sync();  // empty stream
    cin.get();   // pause
}

int main()
{
    cout << "Enter your name: ";
    string str;
    cin >> str;
    cout << str << endl;

#if PAUSE == 1
    pause1();
#else
    pause2();
#endif
}
Last edited on
You can't claim that it doesn't work by mis-using it. Consider the following:
1
2
3
4
5
6
7
8
int age;
string name;

cout << "Enter your age:";
cin >> age;

cout << "Enter your name: ";
getline( cin, name );

Can I claim that getline() doesn't work?

Also consider, your code is broken if the person tries to enter their name as "John Jacob Jingleheimer Schmidt".

Once the user corrupts the input stream it is the programmer's prerogative to fix it, _not_ various library functions. That is, library functions should not play with the state or contents of a stream unless they are explicitly listed to do it.
---
Last edited on
Dear Dirk

I might as well mention Nazis now so that this pathetic squabble will end. Get a life.

Bye.
Sorry, Duoas. I was out of line.
Last edited on
Thank you guys :)

Both of your pause functions work!

Thank you once again guys :)
Topic archived. No new replies allowed.