system(pause) using g++ compiler

VBStudent
VBStudent used Ask the Experts™
on
I am looking for a function that will pause the screen and prompt the user to "Press any key to continue.." that will work on the g++ compiler. In MSVC system("pause") works just fine, however, g++ doesn't like that at all. Is there a comparable function for use with the g++ compiler. I've tried writing my own, but have had mixed success. This is the function I wrote myself, however, the screen has a tendency to require the user to hit "Enter" only, and sometimes requires it twice depending on the output previous to invoking the function call.


void pause(void)
{
     cout << "\nPress the Enter key to continue....\n";
     cin.ignore();
     cin.get();
}    
     

Any help would be greatly appreciated! If there is an internal/built in function, that would be better yet.
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
jkr
Top Expert 2012

Commented:
What about just using

void pause(void)
{
    string s;
    cout << "\nPress the Enter key to continue....\n";
    cin >> s;
}    

?

Commented:
What OS are you using the g++ compiler on?
It should work if it's Windows or DOS.

Commented:
When you say it doesn't work, exactly what happens, when it doesn't work on g++.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

Author

Commented:
I'm running the program on Red Hat linux and Debian linux. It behaves the same way on both those versions of linux. Particularly, what is happening is this. If the last output to the screen is a carriage return (ie. endl at the end of a cout)
"Press Enter key to continue.."  //comes up on screen

press the enter key once //one line is fed but the screen is still paused.

press the enter key a second time.

next record is displayed.

This is the code that the function is used with:

for (i = 1; i < 100; i++)
{
cout << temp << endl << endl;
pause(); //pause the screen function
}

void pause(void)
{
    cout << "\nPress the Enter key to continue....\n";
    cin.ignore();
    cin.get();
}    
   
EOL

Commented:
Look out for low level console APIs on your OS of choice.

And here the wierd answer of the day. Split a thread from your  process, let the displaying thread sleep, set up SDL input for the other, and invoke the sleeping display thread upon any keyboard input. Hm, just about the easiest solution.

Alternatively, have you considered to press the "Pause" key on your keyboard?

Commented:
What about:

system("pause");

Commented:
sorry ... doesn't work on g++
Disregard my comment

Commented:
>>sorry ... doesn't work on g++

It's not that it doesn't work on g++.  It's that it doesn't work on Linux/Unix.

g++ is the compiler, and if you use the Windows version, the command works just fine.
jkr
Top Expert 2012

Commented:
Have you tried

void pause(void)
{
   string s;
   cout << "\nPress the Enter key to continue....\n";
   cin >> s;
}    

as I suggested about a day ago?

Author

Commented:
I've discovered one more aspect to the problem. If the pause function is called from with an iteration (in a while loop or for loop) the output ignores the function completely but stops properly on the second iteration.
For example

for (i = 1; i < 20; i++)
{
    cout << myObject << endl;
    pause();
}
It ignores the pause routine for i = 1, but will pause for
2 <= i <= 20 without any problems

On the same hand, if the call to pause() is not nested in an iterative loop, it works just fine on the first instance.

On a side note, I am assuming there is no built-in function for this since nobody has suggested it. Is that correct? In other words, no equivalent to system("pause") for g++ running on linux??
Top Expert 2012
Commented:
IIRC, there is no "pause" in Linux...

Author

Commented:
Both of jkr's comments answered both parts of my question. The other comments were equally appreciated. If I could split the points among the other respondant's, I would, however, jkr answered my nagging question as to whether or not there was a linux equivalent to system("pause")(even though that was secondary). Thank you to all that responded. I do appreciate it.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial