C++/clock

Advertisement


Question
hello sir,,i made this clock program on my own,,
i need to know how to exit from this program,,,nd goto to
another screen say to the function "mainmenu()",i cant sop
the execution of the program,,,can u pls help me??
void clock()
     {
      int i,j,k,n;
      char a[2][5],b[7][10];
      clrscr();
      gotoxy(25,5);
      cout<<"Enter the current : ";
      cin>>i;
      gotoxy(47,5);
      cout<<":";
      cin>>j;
      gotoxy(50,5);
      cout<<":";
      cin>>k;
      clrscr();
  q:
       gotoxy(23,6);
       cout<<"Enter the day no. according as : ";
       gotoxy(30,7);
       cout<<"1 --->  SUNDAY";
       gotoxy(30,8);
       cout<<"2 --->  MONDAY";
       gotoxy(30,9);
       cout<<"3 --->  TUESDAY";
       gotoxy(30,10);
       cout<<"4 --->  WEDNESDAY";
       gotoxy(30,11);
       cout<<"5 --->  THURSDAY";
       gotoxy(30,12);
       cout<<"6 --->  FRIDAY";
       gotoxy(30,13);
       cout<<"7 --->  SATURDAY";
       gotoxy(55,6);
       cin>>n;
  if( n>=8 || n==0)
  {
     gotoxy(23,14);
     cout<<"Please enter the right option as follows";
     clrscr();
     goto q;
  }
  strcpy(a[0],"A.M");
  strcpy(a[1],"P.M");
  strcpy(b[0],"SUNDAY");
  strcpy(b[1],"MONDAY");
  strcpy(b[2],"TUESDAY");
  strcpy(b[3],"WEDNESDAY");
  strcpy(b[4],"THURSDAY");
  strcpy(b[5],"FRIDAY");
  strcpy(b[6],"SATURDAY");
  clrscr();
  p:   for(hr=i;hr<24;hr++)
       {
        for(min=j;min<60;min++)
         {
     for(sec=k;sec<60;sec++)
      {
        gotoxy(32,5);
        cout<<"DIGITAL CLOCK";
        gotoxy(39,6);
        if(i<12)   cout<<setw(8)<<a[0];
        else       cout<<setw(8)<<a[1];
        gotoxy(33,7);
        cout<<b[n-1];
        gotoxy(31,6);
        cout<<hr;
        gotoxy(33,6);
        cout<<":";
        gotoxy(34,6);
        cout<<min;
        gotoxy(36,6);
        cout<<":";
        gotoxy(37,6);
        cout<<sec;
        for(long i=0;i<300000000;i++)
   {
   }
        clrscr();
      }
     i=0;
         }
        j=0;
       }
    if(n==7)
  n=1;
  else
  n++;
  k=0;
  goto p;
  getch();
  }

Answer
Henry, the clock function never returns because, after these three nested loops are done

p: for(hr=i;hr<24;hr++)
    {
        for(min=j;min<60;min++)
        {
            for(sec=k;sec<60;sec++)


The program calls goto p; to go back to do the nested loops over again. The nested loops themselves will take a long time to finish, depending on what you set the  i, j, and k variables to.

If you need this function to run while something else is happening, you will need to put it into its own thread. That is usually quite an advanced technique.

If you want to make the function exit when the user presses a keyboard key, you can add a kbhit() or _kbhit() function call after
the for(long i=0;i<300000000;i++) loop.
   
Say something like this:

if (_kbhit()) return;

That will exit out of the function if the user presses a key. Another idea is to check what key the user pressed, like this:

if (_kbhit())
{
  char ch = (char)_getch();
  if (ch == 'x') return;
}

Also, instead of the long for loop of 300000000, why not use a Sleep function. The windows Sleep(1000) will sleep for 1 second, and your timing will be more accurate.

On my system, both kbhit, and getch start with an underscore character. If you are using an old compiler, like Turbo C++, you will probably not need the underscore.

I hope that helps you.

Best regards
Zlatko

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Zlatko

Expertise

No longer taking questions.

Experience

No longer taking questions.

Education/Credentials
No longer taking questions.

©2012 About.com, a part of The New York Times Company. All rights reserved.