C++ pause command

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

37 Replies - 11211 Views - Last Post: 06 September 2010 - 10:48 AM Rate Topic: -----

#1 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

C++ pause command

Post icon  Posted 05 July 2007 - 04:25 AM

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
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"

Is This A Good Question/Topic? 0
  • +

Replies To: C++ pause command

#2 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: C++ pause command

Posted 05 July 2007 - 04:35 AM

Please look at this
http://www.dreaminco.../snippet582.htm
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,917
  • Joined: 10-May 07

Re: C++ pause command

Posted 05 July 2007 - 05:02 AM

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

http://www.dreaminco...wtopic16391.htm
Was This Post Helpful? 0
  • +
  • -

#4 mattman059  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 538
  • Joined: 23-October 06

Re: C++ pause command

Posted 05 July 2007 - 05:21 AM

....just put " " around pause..like this


system("pause");


Was This Post Helpful? 2
  • +
  • -

#5 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: C++ pause command

Posted 05 July 2007 - 06:12 AM

theholygod do not follow what above two are saying. They are bad alternatives.
Was This Post Helpful? 0
  • +
  • -

#6 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,917
  • Joined: 10-May 07

Re: C++ pause command

Posted 05 July 2007 - 06:14 AM

View PostXing, on 5 Jul, 2007 - 06:12 AM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#7 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: C++ pause command

Posted 05 July 2007 - 07:00 AM

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: 05 July 2007 - 07:01 AM

Was This Post Helpful? 0
  • +
  • -

#8 no2pencil  Icon User is offline

  • Original Digital Gansta
  • member icon

Reputation: 4466
  • View blog
  • Posts: 24,917
  • Joined: 10-May 07

Re: C++ pause command

Posted 05 July 2007 - 07:13 AM

View PostXing, on 5 Jul, 2007 - 07:00 AM, said:

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.

View PostXing, on 5 Jul, 2007 - 07:00 AM, said:

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?
Was This Post Helpful? 0
  • +
  • -

#9 Xing  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 19
  • View blog
  • Posts: 725
  • Joined: 22-July 06

Re: C++ pause command

Posted 05 July 2007 - 07:41 AM

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: 05 July 2007 - 07:42 AM

Was This Post Helpful? 0
  • +
  • -

#10 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 199
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: C++ pause command

Posted 05 July 2007 - 08:33 AM

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:
		cout<<"Press any key to continue...";
	cin.ignore(0,'\n');
	getch();


Was This Post Helpful? 0
  • +
  • -

#11 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: C++ pause command

Posted 05 July 2007 - 11:41 AM

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?
Was This Post Helpful? 0
  • +
  • -

#12 William_Wilson  Icon User is offline

  • lost in compilation
  • member icon

Reputation: 199
  • View blog
  • Posts: 4,807
  • Joined: 23-December 05

Re: C++ pause command

Posted 05 July 2007 - 11:58 AM

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

but these are not the best solutions, but they should compile
Was This Post Helpful? 0
  • +
  • -

#13 siylence  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 30
  • Joined: 10-June 07

Re: C++ pause command

Posted 05 July 2007 - 12:20 PM

Try this instead. I mean... this is what I used when I originally had to make the infamous "Hello World!" program:
#include <iostream>
int main(){
std::cout << "Hello world!";
std::cin.get();
return 0;
}


It will stop until you press enter. If you're not a big fan of "std::" then use this one:
#include <iostream>
int main(){
using namespace std;
cout << "Hello world!";
cin.get();
return 0;
}

I wouldn't use the pause or sleep method for something like this. As William Wilson said... not the best solutions, but they should compile.
Was This Post Helpful? 0
  • +
  • -

#14 theholygod  Icon User is offline

  • D.I.C Regular

Reputation: 7
  • View blog
  • Posts: 327
  • Joined: 05-February 06

Re: C++ pause command

Posted 05 July 2007 - 12:21 PM

they dont compile for me.

keeps saying the function is undeclared.

*edit*

thanks siylence! those work perfectly. Please could you explain why they work?

This post has been edited by theholygod: 05 July 2007 - 12:23 PM

Was This Post Helpful? 0
  • +
  • -

#15 cloudy_day  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 28
  • Joined: 05-July 07

Re: C++ pause command

Posted 05 July 2007 - 01:52 PM

Probably been answered but I didn't read topic.

system("PAUSE");



Sleep with also work, as will using a cin statement but whatever.

This post has been edited by Welli: 05 July 2007 - 01:56 PM

Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3