How to

How do you delay a program before moving on, here is my code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <fstream>

using namespace std;

int main( int argc, char* argv[] )
{
	ofstream outFile( "Results.txt" );

	int x = 0, y = 0, m = 0;
cout << "Hello, this program will find the multiples of x below y" << endl;
system("pause");
cout << "Please enter the value of x: " ;
cin >> x;
cout << "Please enter the value of y: " ;
cin >> m;
cout << "Now the computer will find all the multiples of x below y and will write it into a file" << endl;
system("pause");
fopen("Results.txt","r+");
while(y <= m)
{
y += x;
cout << y << endl;

  
    outFile << y << endl;
    
 
}
outFile.close();
return 0;
}


I want to delay it here
1
2
cout << "Now the computer will find all the multiples of x below y and will write it into a file" << endl;
system("pause");

I want to replace the system("pause") into a 5 second delay
closed account (3qX21hU5)
Are you on a windows or *Nix system? Because #include <windows.h> with Sleep(milliseconds) in Windows or #include <unistd.h> with sleep(milliseconds) in Unix is what I think you are looking for.


For example on windows this works

1
2
3
4
5
6
7
8
9
10
11
// Waits 10 seconds before printing hello
#include <iostream>
#include <windows.h>

using namespace std;

int main()
{
    Sleep(10000);
    cout << "Hello" << endl;
}

Not to sure about the *Nix header since I never use it so if i'm wrong please correct me.
Last edited on
Windows
A very quickly written, system independent version.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <ctime>

void Delay( const unsigned int secs )
{
  const clock_t start = clock();
  clock_t end = clock();

  while( ( end - start )/CLOCKS_PER_SEC < secs )
  {
    end = clock();
  }
}

int main( int argc, char* argv[] )
{
  Delay( 5 );

  std::cout << "5 seconds have passed";
}
Last edited on
thx
closed account (3qX21hU5)
Hutches version is much better if you have the need to be system independent (Which you should be in almost all release software) but if you are just doing a assignment that only calls for windows you can use the sleep function.
iHutch105 version does polling, it will fry your processor.
iirc, `clock()' does not measure wall time, but user time.

By the way, `sleep()' takes the amount of seconds.
closed account (3qX21hU5)
By the way, `sleep()' takes the amount of seconds.

No it doesn't, at least not on windows. Not sure about *Nix, but I know it takes ms on windows

1
2
3
VOID WINAPI Sleep(
  _In_  DWORD dwMilliseconds
);


http://msdn.microsoft.com/en-us/library/windows/desktop/ms686298(v=vs.85).aspx
ne555 wrote:
iHutch105 version does polling, it will fry your processor.

Yes, it polls. I wouldn't advising using it for a bigger scale project but it shouldn't matter all that much given the OP's original code. "Fry" is a strong term. ;-)

Unsure on the user vs. wall clock. I'll have to look into that when I get more time.
@Zereo: c++ is case sensitive.
closed account (3qX21hU5)
Doh sorry about that ;p
Topic archived. No new replies allowed.