help

Started by
7 comments, last by markg 16 years, 5 months ago
I was wondering how to make a program that count's to a certin number to pause before counting to the next number. I put in sleep(1000) for pause 1 sec. I keep receiving a message by the compiler saying undeclared identifier.
Advertisement
You must #include <Windows> if you need to use 'Sleep()', assuming C++, and assuming a Windows operating system.
What platform/compiler/language do you use?

Show us your code, if it is short enough.
Here's how to do it:

First off all , create a function called "delay".

void Delay(int for_how_long){int counter=0;while(counter<for_how_long){//wait..counter+=1;}return;}


In your main(..) , add the following code:

int value=0;int delay=0;cout<<"Give me a number!"<<endl;cin >> value;cout<<"Set delay  (in seconds!. ie : 1,2,3..)!"<<endl;cin >> delay;delay *=1000;//seconds to milisecondsfor (int i=0; i<value;i++)//print the numbers{cout <<"Number : "<<i<<" , and waiting for : "<<value/1000<<" second(s)!"<<endl;Delay(delay);//this will cause delay..}


Thats it !

[Edited by - 3Dgonewild on November 24, 2007 11:03:37 AM]
Quote:Original post by 3Dgonewild
Here's how to do it:

First off all , create a function called "delay".

*** Source Snippet Removed ***

In your main(..) , add the following code:

*** Source Snippet Removed ***

Thats it !


Bad idea. Some compilers could optimise your delay loop away, since it does nothing and has no side effects. Secondly, your code requires a constant which relates only to the development machine. On a faster machine, the loop will end quicker. On a slow machine it will take longer. In any case there is certainly an API call that suspend your program for a while, allowing other programs to run.
You never increment 'counter' in your while loop, 3Dgonewild. He'd get stuck in an infinite loop. Not to mention that the using your code and putting Delay(1000) wouldn't run for 1000 millaseconds(one second), it'd run for 1000 increments, which on a good computer would be over in less than 1 millasecond (or at least drastically less than one second).

Unless by '//wait..' you mean to add 'Sleep(1)' or some other function there?
@rip-off:

Well , i wrote the delay() function to help him understand what the function does.
In my example , i could just tell him to include windows header , and call sleep() function.
When i started with programming(You helped me a lot btw , so you must remember my stupid questions..lol) , i wanted clear(= code examples)/well explained answers...
Well , my answer wasn't *well explained* or whatever , but i guess he got the message!

Quote:Original post by Servant of the Lord
You never increment 'counter' in your while loop, 3Dgonewild. He'd get stuck in an infinite loop.

OOps , sorry :)..
Quote:Original post by markg
I was wondering how to make a program that count's to a certin number to pause
before counting to the next number. I put in sleep(1000) for pause 1 sec. I keep receiving a message by the compiler saying undeclared identifier.


Quote:Original post by rip-off
What platform/compiler/language do you use?

Show us your code, if it is short enough.


I am using that microsoft visual c++.net compiler

This topic is closed to new replies.

Advertisement