I don't know how to pause the screen

Jan 15, 2008 at 5:55pm
I have tried to use a batch file to pause between an if and else clause and as soon as I hit the enter key after I enter the information I want to continue the program it disappears.

It is supposed to say, Enter your score: , and then you enter any number you wish. Then it is supposed to display either of the following messages, You got 500 or less. Nothing to brag about. Or You got over 500. Nice score.

But as soon as I hit the enter key the whole program disappears. Does anybody know how to stop this from happening?
Jan 15, 2008 at 9:17pm
after you typeded cin >> ....; on the following line type in
cin.get(); and continue with your code
this will pause the screen after you had pressed enter
Last edited on Jan 17, 2008 at 9:50pm
Jan 16, 2008 at 8:17am
hazda has a good way but i got a way that might be simpler.
Using the command "system("pause") BUT that would leave the message "Press any key to continue ..." So you use this command "system("pause>nul")" and it will pause the screen with-out any message.

kinda the way you would script a batch file but like this
1
2
3
4
5
6
7
8
9
10
11
//example
#include <iostream>
using namespace std;

int main ()
{
      cout << "Hello World!";
      system("pause>nul");

      return 0;
}


This will print out :

Hello World!

and pause with no message.

Hope that helped :)
by the way what's you email ?
Last edited on Jan 16, 2008 at 8:17am
Jan 16, 2008 at 4:34pm
The thing is that system("pause") will NOT work for linux, therefore it is not a portable method.
Jan 16, 2008 at 4:47pm
this is how I do it. Why is there an 'm' in cin.get hazda?

added is char ch, cin.ignore() and cin.get() (empty paren's). the window closes early due to a keyboard buffer matter. See if this works for you.

int main()
{
char ch;
int score;

// Get the score.
cout << "Whats your score? ";
cin >> score;

/* had a little problem with mixing cin << and cin.get(). The
window closes never displaying resulting message. I'm
repairing by using cin.ignore() by skipping next character */
cin.ignore();

// Display result.
cout << "You input " << score << endl;
cin.get(ch);
return 0;
}
Jan 16, 2008 at 7:44pm
thanks to all of you! You have helped me extremely!
Jan 16, 2008 at 7:50pm
My e-mail is diablosilvermoon@yahoo.com to those who want to know. And thanks again for the help.
Jan 17, 2008 at 9:56pm
ddolla why do u have to include the char ch in cin.get() resultin in cin.get(ch)? what difference does it make to the program if u dont declare the char ch and use cin.get() (empty paren's)? pls explain why
Jan 20, 2008 at 2:50am
What I do is before the statement "return 0;" I enter the following phrase,
system("pause")
Works every time
Feb 22, 2008 at 6:15pm
hellow every body, i'm mo3taz from egypt
no body wrote any thing to me till now but
i've a way to stop the window may be similsr to jesse's one
my e-mail is: mo3taz_kilany@hotmail.com

1
2
3
4
5
6
7
8
# include <iostream.h>
# include <conio.h>           // the header file includes the function getch()
int main()
{
cout << "hello world\n";
getch();                             // make the program waits for any key to be 
return 0;                           // pressed to quit
}

i neeeeeeed comments.
Last edited on Feb 22, 2008 at 6:17pm
Feb 23, 2008 at 3:32pm
The problem is three-fold:

1) As already mentioned, "pause" is a fairly DOS/Windows-ish thing to do. On linux, you'd use the bash script:
read -n 1 -p "Press any key to continue . . ."

2) Using system() is a security hole. Use it only for homework.

3) The way it works is different than the way user-input normally works. Normally, input is "line-buffered", meaning that the user _must_ press ENTER to finish giving input. The pause command(s) are not line-buffered. Pressing just about any key will work.

Turning off line-buffering (and a few other things) is a fairly involved, platform-dependent task (but it isn't hard at all). However, for _standard_C++_ you can easily write your own pause:
1
2
3
4
5
6
void pause() {
  cout << "Press ENTER to continue..." << flush;
  cin.clear();  // use only if a human is involved
  cin.flush();  // use only if a human is involved
  cin.ignore( numeric_limits<streamsize>::max(), '\n' );
  }

Thanks to _dirk for the suggestion for flushing the stream. This will play havoc if you are _not_ connected to a human.

If y'all want to know how to do it the non-line-buffered way, let me know. (And specify what platform you are using.)
Topic archived. No new replies allowed.