-4

Possible Duplicate:
Exit a loop at anytime

Sorry but I have another problem. If I change 'time_t end = time(0) + 1;' to 'time_t end = time(0) + 5;'... Then my program will not exit the loop straight away but exits after 5 seconds. How can I change it to exit the loop straight away? Let's say if it were to be a minute, cause I'm creating a program, a parking meter where you can exit the loop anytime to claim for refunds. Reference to the code below :

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main(void)
{
    int b=0, i;
    int seconds;
    printf("\nEnter number of seconds : ");
    scanf("%d", &seconds);
    while (b==0)
    {
        b=kbhit();
        for(i=1;i<=seconds;i++)
        {
            while (b==0)
            {
                b=kbhit();
                time_t end = time(0) + 5; //5 seconds delay.
                while(time(0) < end)
                {
                    b=kbhit();
                }
                seconds -= 5;
                printf("Number of seconds left : %d\n", seconds);
                b=kbhit();  
            }
        }
        if(seconds == 0)
        {
            exit(0);
        }
        b=kbhit();
    }
    printf("Number of remaining seconds left : %d\n", seconds);
}
Community
  • 1
  • 1
Jitzu Zu
  • 71
  • 1
  • 1
  • 4

1 Answers1

0

Using a break is a better solution to exit the loop

Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331