The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > High Level Programming
Google UNIX.COM


High Level Programming Post questions about C, C++, Java, SQL, and other programming languages here.

Reply
 
Submit Tools LinkBack Thread Tools Display Modes
  #1  
Old 07-29-2004
Registered User
 

Join Date: May 2004
Posts: 66
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!
Reply With Quote
Forum Sponsor
  #2  
Old 07-29-2004
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
declare your variables.
Reply With Quote
  #3  
Old 07-29-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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);
}
Reply With Quote
  #4  
Old 07-29-2004
Registered User
 

Join Date: May 2004
Posts: 66
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?
Reply With Quote
  #5  
Old 07-29-2004
Registered User
 

Join Date: May 2004
Posts: 66
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!
Reply With Quote
  #6  
Old 07-29-2004
...@...
 

Join Date: Feb 2004
Location: NM
Posts: 4,274
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.
Reply With Quote
  #7  
Old 07-30-2004
Registered User
 

Join Date: Jul 2002
Location: new york
Posts: 1,025
Quote:
Oh, and by the way, time_t is an arithmetic type.
oooh i see. i didnt know that

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.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 06:23 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0