i want to write a program which display alert message whenever 15 mins are over.But i am not finding solution of my problem using waitforexit() or delay() functions.

#include<time.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
void main(void)
{

time_t timer;
struct tm *tblock;
time_t t;
//timeout=18000;
	/* gets time of day */
	timer = time(NULL);
	/* converts date/time to a structure */
	tblock = localtime(&timer);
	t = time(NULL);
	printf("Local time is: %s", asctime(tblock));
	printf("The number of seconds since January 1, 1970 is %ld",t);


	while(1)
	{
		sleep(150);
		printf("\n\n\n\n**********************times up************************\n\n");
		getch();
	};


}

Dani AI

Generated

Short, practical guidance (Turbo C++ 4.5 / DOS era) — the right idea is to compare wall-clock seconds and avoid blocking calls that stop the timer.

As suggested, save a time snapshot and check the difference; for a half‑hour use 30 * 60 seconds. Important Turbo C++ notes: delay() from <dos.h> takes milliseconds (not seconds) and on a 16‑bit build its argument is typically limited (~65535 ms), so don’t pass very large values to delay() in one call. Also avoid calling blocking functions like getch() while you expect the timer to continue — use kbhit() for non‑blocking input if you need a cancel key. Don’t name a variable true (reserved literal in C++); use running or similar.

Example pattern (keeps CPU usage low, allows cancellation, safe for long waits):

#include <stdio.h>
#include <time.h>
#include <conio.h>
#include <dos.h>   /* delay() */

int main(void)
{
    time_t end = time(NULL) + 30 * 60; /* 30 minutes */
    puts("Timer started (30 minutes). Press '*' to cancel.");
    while (time(NULL) < end) {
        if (kbhit()) {
            int c = getch();
            if (c == '*') {
                puts("Cancelled.");
                return 0;
            }
        }
        delay(500); /* milliseconds; yields CPU */
    }
    puts("\n*** 30 minutes elapsed ***\a");
    return 0;
}

Troubleshooting tips:

  • If you tried sleep(150) earlier, note sleep() is POSIX/Windows — not standard in Turbo C++; delay(150) would be 150 ms, not 150 s.
  • time() is wall clock: changing system time (or DST adjustments) will affect it. If you need a monotonic timer you’ll need platform-specific clock sources not available in plain Turbo C++.
  • If the program must do significant work while timing, integrate the time check into the main loop (or use a modern compiler/OS that supports timers/threads).

Recommended Answers

All 2 Replies

Do you want your program to do other things in the meantime? If you do, then put the alert messages in another thread, but your compiler may not support multi-threaded programs. So an alternative might be to use a loop

time_t t1, t2; // time variables
double dif;
t1 = time(0); // get current time
while(true)
{
   t2 = time(0);
   dif = diftime(t2,t1);
   if( dif >= (double)(15 * 60) ) // if 15 minutes expired
   {
      // display alert message
      //
      // reset timer
      t1 = t2;
   }
   // do other stuff here
}
#include<time.h>
#include<stdio.h>
#include<dos.h>
#include<conio.h>
#include<stdlib.h>
void main(void)
{
int true=1,num;
char ch;

time_t t1, t2; // time variables
double dif;
t1 = time(0); // get current time
printf("\n__________________________counting time____________________");
while(true)
{  num=0;
	t2 = time(0);
	dif = difftime(t2,t1);
	if( dif >= (double)(1 * 60) ) // if 15 minutes expired
	{
																												// display alert message
		printf("\n--------------\a-------------times up!----------------------------");
		printf("\n----------------------------thanks!-----------------------------");
		getch();
																												// reset timer
		t1 = t2;
		clrscr();
		num=1;
	if(num==1)
		{
		printf("\nEnter * to close app:");      														 //condition to limit the loop
		ch=getche();
		if(ch=='*')
		exit(0);
		}
	}
}

}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.