Please Help!


 
Thread Tools Search this Thread
Top Forums Programming Please Help!
# 1  
Old 07-29-2004
Question Please Help!

I'm trying to comple this small program to waste CPU usage. The code is as follow:
Code:
#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:

Code:
 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!
# 2  
Old 07-29-2004
declare your variables.
# 3  
Old 07-29-2004
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:
Code:
#include <time.h>

#define SLEEPTIME 5

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

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

# 4  
Old 07-29-2004
Quote:
Originally posted by jim mcnamara
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:
Code:
#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?
# 5  
Old 07-29-2004
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!
Smilie
# 6  
Old 07-29-2004
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.
# 7  
Old 07-30-2004
Quote:
Oh, and by the way, time_t is an arithmetic type.
oooh i see. i didnt know that Smilie

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.
Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question