Member Avatar for vs49688

Hi Guys. I am relatively new to C++ so i may seem a bit inexperienced, but please bear with me.

I am doing a little c++ project to help increase my experience and i have come to a stand still. (I have defined loopcount earlier on in the program)

while (loopcount > 0) //It is meant to be a infinite loop
{
cout << "100101100011110101010100000001010101000010101001010101000100"
       << "100000000001011110111101100001001011011001101010001011010110"
       << "010110010101010101011110101010100001111101100001000100101010"
       << "010101010101011011010101010101000000000001111111111000110000";
}

I need each digit to appear on the screen one at a time every 0.25 seconds. Is there any possible way to do this.

Any help would be greatly appreciated.

Zane

Recommended Answers

All 13 Replies

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

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 <ctime> library.

nevermind. I misunderstood the previous post.

Member Avatar for vs49688

Thanks Guys. I really needed this.

Member Avatar for vs49688

Oops. I am getting this error: "error C2601: 'sleep' : local function definitions are illegal"

Here is the code im using

void sleep( clock_t wait )
{
clock_t goal;
goal = wait + clock();
while( goal > clock() );
       {
	cout << "100101100011110101010100000001010101000010101001010101000100"
	       << "100000000001011110111101100001001011011001101010001011010110"
	       << "010110010101010101011110101010100001111101100001000100101010"
	       << "010101010101011011010101010101000000000001111111111000110000";
        }
}

I have no idea how to fix this.

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.

@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..

I am also not able to understand this clock()-t1> [B]25[/B]

Member Avatar for vs49688

Yippee. Thanks vmanes. It was just what i wanted it to do. All I had to do was edit so it would work inside a switch. Thank You So much

@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> [B]25[/B]

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.

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.

I think he asked for 250 miliseconds didn't he? not 25?

Anyway, it shouldn't matter, because there's no guarantee that CLOCKS_PER_SEC is equal to 1000 (I believe on POSIX systems its defined as 1000000)

I also think its a bad idea to use the name 'sleep' too, since its a common 3rd party library name in Windows and *nix, but that's just my opinion :) Although, if you already have the sleep function available, that's probably a better solution anyway (since its not tying up the system in a 'do nothing' loop )


a more portable solution might look like

void think( clock_t milliseconds )
{
    clock_t until = milliseconds * CLOCKS_PER_SEC / 1000;
    until += clock();
    while( clock() < until )
        ; /* do nothing */
}

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

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 :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.