0

I need to loop for a certain amount of time based on user input ( in C Program ).

Example: User says loop for 2 Minutes ( = 120 seconds).

while(time <= 2 Minutes)
{
    do something
}

How would I go about doing this in C? Thanks for your help!!

i_am_jorf
  • 52,428
  • 15
  • 126
  • 218
Jake Z
  • 1,053
  • 1
  • 11
  • 20

2 Answers2

0

You do not need to loop.

Just take a nap. It will sleep for the amount of time and let the processor to do something more useful

Ed Heal
  • 57,599
  • 16
  • 82
  • 120
  • I need to perform some operation as many times as possible within the specific period of time...thus napping is not an option for me! – Jake Z Sep 21 '13 at 20:00
  • 1
    You did not point that out in the question. Besides how long does the operation in the loop take. Perhaps it should periodically ask - Should I abort. – Ed Heal Sep 21 '13 at 20:06
0

If you need to perform some operation as many times as possible within the specific period of time you can use time():

time_t secs = 120; // 2 minutes (can be retrieved from user's input)

time_t startTime = time(NULL);
while (time(NULL) - startTime < secs)
{
    ...
}
LihO
  • 39,598
  • 10
  • 94
  • 164