Use the clock() function. Create a loop and display the next character in the string when the current value of clock minus the original value is >= 25
clock_t t1, t2;
t1 = clock();
while more characters to display
if clock() - t1 > 25
display a character
set t1 = clock()
end if
end loop
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
Put all the digits in an array, one digit per element. In a loop, display a character, then execute some delay mechanism for 1/4 second. Display next element....
A piece of code I believe I saw in another post here (sorry if I'm stealing someone's thunder) that will do the delay for you is:
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Be sure to include the library.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
nevermind. I misunderstood the previous post.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
That error generally occurs when you've not closed of the curly braces of the preceding function. Look for mismatched set of { }.
The code you've posted will not do what you originally asked for help on. It will display the full sequence of 1s and 0s every "wait" milliseconds.
Here's the direction I think you're wanting to take:
#include <iostream>
#include <cstring>
#include <ctime>
using namespace std;
void sleep( clock_t wait );
int main()
{
char digits[] = "10010110001111010101010000000101010100001010100";
unsigned len = strlen( digits );
unsigned i;
for( i = 0; i < len; i++ )
{
cout << digits[i];
sleep( 750 );
}
cout << endl;
return 0;
}
/* Pauses for a specified number of milliseconds. */
void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() )
;
}
Note that the interval between display of characters may not always seem precise, as the output is time consuming and some output gets buffered. Other processes running on your machine may also interfere with the timing.
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
@Ancient Dragon...
i want to ask what does clock function return and in what format and with what units(milliseconds/microsec/sec)
someone told me in this community that if u want the output in seconds divide the value of clock_t variable by CLOCKS_PER_SEC...and u will get the time in seconds..
On MS-Windows and I think *nix too the clock function returns milliseconds. The calculation you cite is correct.I am also not able to understand this
clock()-t1> <strong>25</strong>
That is checking if the time that has expired is 25 or more milliseconds then do something. You said you wanted to print a character every 25 milliseconds, so that's the reason for the 25 in that conditional statement.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
vmanes: your sleep function may pause the program for awhile but at what price? It will probably consume nearly 100% of the cpu time and not allow much time for other processes to work. That function is not very operating-sytesm friendly.
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
vmanes: your sleep function may pause the program for awhile but at what price? It will probably consume nearly 100% of the cpu time and not allow much time for other processes to work. That function is not very operating-sytesm friendly.
Agree that do-nothing loops are not particularly good from the standpoint of good program behavior in the overall system. The OP stated he's just starting out in C++, so performance may not be as much an issue as learning how to make things work.
A brief googling for C++ (or C) sleep functions show that there does not seem to be any one right way to get this sort of a pause, and the various sleep() or Sleep() (or maybe even SLEEP() ) functions are OS and/or compiler dependent. And they generally depend on the OS's scheduler to wake the program up at some time not less than the specified period, where such wakeup may be later than the desired interval.
Hmm, I think I'll go write a nap( ) function, that puts the program down for some random amount of time, waiting for delivery of milk and cookies.
Val
vmanes
Posting Virtuoso
1,914 posts since Aug 2007
Reputation Points: 1,268
Solved Threads: 228
Hmm, I think I'll go write a nap( ) function, that puts the program down for some random amount of time, waiting for delivery of milk and cookies.
Val
Great idea! I'l join you :)
Ancient Dragon
Retired & Loving It
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343