Link to home
Start Free TrialLog in
Avatar of VBStudent
VBStudent

asked on

system(pause) using g++ compiler

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.
Avatar of jkr
jkr
Flag of Germany image

What about just using

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

?
What OS are you using the g++ compiler on?
It should work if it's Windows or DOS.
When you say it doesn't work, exactly what happens, when it doesn't work on g++.
Avatar of VBStudent
VBStudent

ASKER

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();
}    
   
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?
What about:

system("pause");

sorry ... doesn't work on g++
Disregard my comment
>>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.
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?
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??
ASKER CERTIFIED SOLUTION
Avatar of jkr
jkr
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.