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

Feb 17, 2008 at 2:11pm
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.
Feb 17, 2008 at 5:33pm
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.
Feb 17, 2008 at 7:32pm
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 Feb 17, 2008 at 7:33pm
Feb 18, 2008 at 2:31am
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.
Feb 18, 2008 at 4:06pm
---
Last edited on Feb 19, 2008 at 2:18am
Feb 18, 2008 at 5:57pm
Dear Dirk

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

Bye.
Feb 18, 2008 at 11:32pm
Sorry, Duoas. I was out of line.
Last edited on Feb 19, 2008 at 2:18am
Feb 24, 2008 at 4:42pm
Thank you guys :)

Both of your pause functions work!

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