Can someone tell me what would be the best way to create a delay in my program of about 2 seconds.
Can someone tell me what would be the best way to create a delay in my program of about 2 seconds.
- Buy a slower PC.
- Search the board for answers, this has been asked many times.
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
Code:/* Function: wait_a_moment(int seconds) Purpose: Provide a predetermined pause in the program giving the user enough time to read a message. Continue program execution after time is up without any user interaction. Usage: wait_a_moment(3) - Pauses for 3 seconds wait_a_moment(5) - Pauses for 5 seconds */ #include <time.h> void wait_a_moment(int seconds); void wait_a_moment(int seconds) { clock_t endtime = clock() + seconds * CLOCKS_PER_SEC; while ( ( clock() < endtime ) ); }
The world is waiting. I must leave you now.
shadow,
I appreciate your help
I have already tried using that code and I get a bunch of errors
what could I possibly be leaving out
Here is what I have:
#include <stdio.h>
#include <tjpbase.h>
#include <time.h>
using namespace std;
void main()
{
init_motortjp();
motorp (0,50);
motorp (1,50);
void wait_a_moment(int seconds);
void wait_a_moment(int seconds)
{
clock_t endtime = clock() +seconds * CLOCKS_PER_SEC;
while ( ( clock() < endtime ) );
}
and the errors:
!E straight.c(20): syntax error; found `seconds' expecting `;'
!E straight.c(20): warning: expression with no effect elided
!E straight.c(20): syntax error; found `)' expecting `;'
!E straight.c(20): illegal statement termination
!E straight.c(20): skipping `)'
!E straight.c(22): undeclared identifier `clock_t'
!E straight.c(22): warning: expression with no effect elided
!E straight.c(22): syntax error; found `endtime' expecting `;'
!E straight.c(22): too many errors
Your main function doesn't return a value.void main()
{
init_motortjp();
motorp (0,50);
motorp (1,50);
You used void main. Don't.
Your main function doesn't have a closing brace.
You put your secondary function's prototype inbetween your main function, and the function itself.
The following program demonstrates how to do all of these things, and use the code I submitted.
[EDIT]Code:#include <stdio.h> #include <time.h> void wait_a_moment(int seconds); int main(void) { printf("Pausing for 3 seconds.."); wait_a_moment(3); printf("Pausing for 5 seconds.."); wait_a_moment(5); return 0; } void wait_a_moment(int seconds) { clock_t endtime = clock() + seconds * CLOCKS_PER_SEC; while ( ( clock() < endtime ) ); }
Compile this code as is.
[/EDIT]
The world is waiting. I must leave you now.
>using namespace std;
What language are you writing in..? Make your mind up, is it C or C++?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]