Please Help!

I'm trying to comple this small program to waste CPU usage. The code is as follow:


#include <time.h>

#define SLEEPTIME 5

time_t start, end;
start=time(NULL);
end=start+SLEEPTIME;

while(end > start){
     start=time(NULL);
}     

and i got the following errors:

 g++ waste.cpp
waste.cpp:6: error: ISO C++ forbids declaration of `start' with no type
waste.cpp:6: error: conflicting types for `int start'
waste.cpp:5: error: previous declaration as `time_t start'
waste.cpp:7: error: ISO C++ forbids declaration of `end' with no type
waste.cpp:7: error: conflicting types for `int end'
waste.cpp:5: error: previous declaration as `time_t end'
waste.cpp:9: error: syntax error before `while'  

what could be wrong? if there's a better way to do this, please suggest.
Thank you much!

declare your variables.

I think the compiler is confused - your code, as posted, looks reasonable. It looks like the compiler has already seen start referenced before. start is not a reserved word in C++. It also looks like variables are declared.

try something like this:

#include <time.h>

#define SLEEPTIME 5

time_t start=time(NULL);
time_t end=start+SLEEPTIME;

while(end > start){
     start=time(NULL);
}     

Thanks for your suggestion, but i got 1 error this time when compling the above code:
try.cpp:8: error: syntax error before `while'

could it be that SLEEPTIME is of int type and assigning to time_t variable is illegal?

As always, thank you so much Driver!
Yes, I am a newbie and still learning by way through trying out different applications.
I just wanted a small program to waste cpu and yes portability a concern here. But i'll take up on your suggestion on trying it out with sleep()

Cheers!
:slight_smile:

Ooooops. I assumed that was a snippet, not the whole thing...
All C, C++ code requires main() somewhere. That's the place where the program starts running.

oooh i see. i didnt know that :eek:

its been a while since ive played with c.

and as stated before, you need the main() function.

i thought it was just a "snippet" also.

OOPPPSSSS... i completely forgot about main()... :smiley: Thank you guys!

and i've taken on Driver's note on using sleep() function instead of SLEEPTIME. and it's working good!

cheers! (It's FRiDAY!!!)))

:smiley: