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
  • 53,608
  • 15
  • 131
  • 222
Jake Z
  • 1,113
  • 1
  • 12
  • 22

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
  • 59,252
  • 17
  • 87
  • 127
  • 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
  • 41,190
  • 11
  • 99
  • 167