Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,520 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,391 people online right now. Registration is fast and FREE... Join Now!




C++ pause command

3 Pages V  1 2 3 >  
Reply to this topicStart new topic

C++ pause command

theholygod
5 Jul, 2007 - 03:25 AM
Post #1

D.I.C Head
**

Joined: 5 Feb, 2006
Posts: 223


My Contributions
So im trying to get back into C++. again.

I typed up the hello world tutorial, and remembered that as soon as i hit "compile and run", the command line window would pop up for a split second and then close again (seeing as it has finished what it has done)

i remembered that i used to use a pause command of some kind. i tried
CODE
system(PAUSE);

but this didnt work.

My computing teacher suggested i put in an endless loop (which sounds pretty stupid and inneficient).

Does anyone know the pause command im looking for? i remember it said "please press enter to continue"
User is offlineProfile CardPM
+Quote Post

Xing
RE: C++ Pause Command
5 Jul, 2007 - 03:35 AM
Post #2

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 4 times
Dream Kudos: 1575
My Contributions
Please look at this
http://www.dreamincode.net/code/snippet582.htm
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ Pause Command
5 Jul, 2007 - 04:02 AM
Post #3

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
Rather than use the system(PAUSE) like you did, I would issue the sleep directly within the code.

http://www.dreamincode.net/forums/showtopic16391.htm
User is online!Profile CardPM
+Quote Post

mattman059
RE: C++ Pause Command
5 Jul, 2007 - 04:21 AM
Post #4

D.I.C Regular
Group Icon

Joined: 23 Oct, 2006
Posts: 361


Dream Kudos: 175
My Contributions
....just put " " around pause..like this


CODE

system("pause");

User is online!Profile CardPM
+Quote Post

Xing
RE: C++ Pause Command
5 Jul, 2007 - 05:12 AM
Post #5

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 4 times
Dream Kudos: 1575
My Contributions
theholygod do not follow what above two are saying. They are bad alternatives.
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ Pause Command
5 Jul, 2007 - 05:14 AM
Post #6

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Xing @ 5 Jul, 2007 - 06:12 AM) *

theholygod do not follow what above two are saying. They are bad alternatives.

Could you elaborate so I can understand why they are bad alternatives?
User is online!Profile CardPM
+Quote Post

Xing
RE: C++ Pause Command
5 Jul, 2007 - 06:00 AM
Post #7

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 4 times
Dream Kudos: 1575
My Contributions
Few reasons
1. There is a difference between sleep and pause.
2. sleep is non-portable like the parameters of system function.
3. You must include a headers you probably don't need: stdlib.h or cstdlib, someother for sleep.
4. system is very expensive and resource heavy function call. It's like using a bulldozer to open your front door. It works, but the key is cleaner, easier, cheaper. What system() does is:

#suspend your program

#call the operating system

#open an operating system shell (relaunches the O/S in a sub-process)

#the O/S must now find the PAUSE command

#allocate the memory to execute the command

#execute the command and wait for a keystroke

#deallocate the memory

#exit the OS

#resume your program

What I have showed you is much cleaner and portable way.

This post has been edited by Xing: 5 Jul, 2007 - 06:01 AM
User is offlineProfile CardPM
+Quote Post

no2pencil
RE: C++ Pause Command
5 Jul, 2007 - 06:13 AM
Post #8

My fridge be runnin OH NOEZ!
Group Icon

Joined: 10 May, 2007
Posts: 7,123



Thanked: 76 times
Dream Kudos: 2425
Expert In: Goofing Off

My Contributions
QUOTE(Xing @ 5 Jul, 2007 - 07:00 AM) *

2. sleep is non-portable like the parameters of system function.

This makes them equal, therefor not a reason to use one over the other.

QUOTE(Xing @ 5 Jul, 2007 - 07:00 AM) *

4. system is very expensive and resource heavy function call. It's like using a bulldozer

This is exactly why I would use sleep over system("pause");

So again, why is sleep a bad alternative to system("pause"), other than including a header file?

Which is lesser evil, including an extra header or using a system call?
User is online!Profile CardPM
+Quote Post

Xing
RE: C++ Pause Command
5 Jul, 2007 - 06:41 AM
Post #9

D.I.C Addict
Group Icon

Joined: 22 Jul, 2006
Posts: 723



Thanked: 4 times
Dream Kudos: 1575
My Contributions
I never compared sleep with system. I said sleep and system are bad alternatives as compared to my solution.

This post has been edited by Xing: 5 Jul, 2007 - 06:42 AM
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: C++ Pause Command
5 Jul, 2007 - 07:33 AM
Post #10

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
i agree with not using system("pause"); i have also run into circumstances where it will run before the code is completed.. since the system call can cause an interrupt which halts the code which called it.

I found if you include conio.h that the following works quite well, and allows a single character to be entered, without being visible:
CODE

        cout<<"Press any key to continue...";
    cin.ignore(0,'\n');
    getch();

User is offlineProfile CardPM
+Quote Post

theholygod
RE: C++ Pause Command
5 Jul, 2007 - 10:41 AM
Post #11

D.I.C Head
**

Joined: 5 Feb, 2006
Posts: 223


My Contributions
None of your methods seem to be working for me!

am i using the wrong header? im using iostream

sleep(5); didnt compile (im using bloodshed), system("pause"); didnt do anything at all and Xings method didnt do anything and i didnt understand the code anyway.

Maybe im overlooking something really stupid?
User is offlineProfile CardPM
+Quote Post

William_Wilson
RE: C++ Pause Command
5 Jul, 2007 - 10:58 AM
Post #12

lost in compilation
Group Icon

Joined: 23 Dec, 2005
Posts: 4,101



Thanked: 25 times
Dream Kudos: 3275
Expert In: Java, C, Javascript

My Contributions
they are:
Sleep(5);
System("PAUSE");

but these are not the best solutions, but they should compile
User is offlineProfile CardPM
+Quote Post

3 Pages V  1 2 3 >
Reply to this topicStart new topic
Time is now: 1/7/09 08:22PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month