Need to add pause to existing C++ program

Need to add pause to existing C++ program

Page 1 of 1

9 Replies - 4299 Views - Last Post: 17 July 2009 - 05:28 PM Rate Topic: -----

#1 straygrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-October 08

Need to add pause to existing C++ program

Posted 17 July 2009 - 02:39 AM

I have been asked to add a pause to an existing C++ program.
They do not want to use the Windows builtin scheduler for reasons they won't share with me.
Is the correct way of doing this something like:-
void Sleep(10000);
Is This A Good Question/Topic? 0
  • +

Replies To: Need to add pause to existing C++ program

#2 deery5000  Icon User is offline

  • D.I.C Addict

Reputation: 78
  • View blog
  • Posts: 966
  • Joined: 09-May 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 03:23 AM

NOPE. . .
your teacher doesnt want you to use

system Commands
system("pause");



Try using
 
cin.get()



or
#include "conio.h" 

 _getch();   



but its much better to you cin.get();

have a look at this post
http://www.dreaminco...wtopic30581.htm

Hope this helps
Kevin

This post has been edited by deery5000: 17 July 2009 - 03:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 straygrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-October 08

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 04:07 AM

View Postdeery5000, on 17 Jul, 2009 - 02:23 AM, said:

NOPE. . .
your teacher doesnt want you to use

system Commands
system("pause");



Try using
 
cin.get()



or
#include "conio.h" 

 _getch();   



but its much better to you cin.get();

have a look at this post
http://www.dreaminco...wtopic30581.htm

Hope this helps
Kevin



I am afraid that my question was not clear enough.
We need the program to stop running for a period of time and when that time has elapsed carry on running.
That was why I suggested sleep().
Was This Post Helpful? 0
  • +
  • -

#4 CosmicTruth  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 20
  • Joined: 08-March 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 04:52 AM

View Poststraygrey, on 17 Jul, 2009 - 01:39 AM, said:

I have been asked to add a pause to an existing C++ program.
They do not want to use the Windows builtin scheduler for reasons they won't share with me.
Is the correct way of doing this something like:-
void Sleep(10000);


DON'T use:

include <windows.h>

Sleep(5000);
Was This Post Helpful? 0
  • +
  • -

#5 straygrey  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 04-October 08

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 07:46 AM

A nice explanation as to why would be nice.
Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 07:52 AM

sleep is resource heavy, it calls into the system( i think) or at least a wrapper for it. Otherwords, it is not standard c++ .this is the best way i know how to do this :
#include <ctime>

void timer(double seconds)
{
clock_t x;
x=clock()+seconds*CLOCKS_PER_SEC;
while(clock()<x){}
}


This post has been edited by ImaSexy: 17 July 2009 - 07:54 AM

Was This Post Helpful? 0
  • +
  • -

#7 codefreak.  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 5
  • View blog
  • Posts: 79
  • Joined: 11-July 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 12:31 PM

int count = 1;

while (count <= 1000)
	count++;


Was This Post Helpful? 0
  • +
  • -

#8 Chr1seN  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 66
  • Joined: 02-July 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 12:37 PM

View PostImaSexy, on 17 Jul, 2009 - 06:52 AM, said:

sleep is resource heavy, it calls into the system( i think) or at least a wrapper for it. Otherwords, it is not standard c++ .this is the best way i know how to do this :
#include <ctime>

void timer(double seconds)
{
clock_t x;
x=clock()+seconds*CLOCKS_PER_SEC;
while(clock()<x){}
}


what?
listen what you're saying :P..
1) Sleep is just pausing current thread, this means it uses maximumly 1% of processor while your code tries to execute so fast as it can.. making processor 100%

Sleep doesnt have anything to do with system(), i think you have gotten somewhere wrong, the most inefficient way to sleep is just exact way you showed.
Was This Post Helpful? 0
  • +
  • -

#9 robhilly  Icon User is offline

  • New D.I.C Head

Reputation: 6
  • View blog
  • Posts: 46
  • Joined: 26-May 08

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 02:56 PM

What I used to do to temporarily pause it was just input a character with the same kind of message it would give you from a Microsoft compiler:


char pause()
{
	char c;
	cout << "\nPlease press a button and then hit <Enter>: ";
	cin >> c;

	//return the character pressed incase it is useful:
	return c;
}



Was This Post Helpful? 0
  • +
  • -

#10 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Need to add pause to existing C++ program

Posted 17 July 2009 - 05:28 PM

oh my bad, sorry im not used to using it. But i know that sleep() is ok, but Sleep() is alright too but it is not standard c++
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1