PracticeDev/study_cpp/cpp_primer_source_code/Chapter 5/waiting.cpp

20 lines
616 B
C++
Raw Permalink Normal View History

2022-12-20 17:31:11 +08:00
// waiting.cpp -- using clock() in a time-delay loop
#include <iostream>
#include <ctime> // describes clock() function, clock_t type
int main()
{
using namespace std;
cout << "Enter the delay time, in seconds: ";
float secs;
cin >> secs;
clock_t delay = secs * CLOCKS_PER_SEC; // convert to clock ticks
cout << "starting\a\n";
clock_t start = clock();
while (clock() - start < delay ) // wait until time elapses
; // note the semicolon
cout << "done \a\n";
// cin.get();
// cin.get();
return 0;
}