Can anyone help my Stopwatch? C-Programming Only

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

30 Replies - 5704 Views - Last Post: 20 August 2010 - 05:33 AM Rate Topic: -----

#1 Xcody  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 13-August 10

Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 06:07 AM

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


int main()
{
int superleap, leap, sec, min, hour;

printf("press 's' and enter to start: ");

while(getch() != 's'){
}

for(hour=0; hour<24; hour++)
for(min=0; min<60; min++)
for(sec=0; sec<60; sec++)
for(leap=0; leap<4; leap++)
for(superleap=0; superleap<30; superleap++){



printf("%02d:%02d:%02d", hour, min, sec);
system("cls");


}



getch();
return 0;


}



thats my code. can anyone how to stop and pause it?

This post has been edited by JackOfAllTrades: 13 August 2010 - 06:08 AM
Reason for edit:: Fixed code tags.


Is This A Good Question/Topic? 0
  • +

Replies To: Can anyone help my Stopwatch? C-Programming Only

#2 Allophyl  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 2
  • View blog
  • Posts: 67
  • Joined: 09-August 10

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 06:33 AM

Take a look at this link to get you started.
http://www.cplusplus...clibrary/ctime/
Was This Post Helpful? 0
  • +
  • -

#3 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 06:38 AM

Generally to exit out of deep nested loops people either find themselves using a flag or the infamous goto. However I suggest a different solution: make your timer a function, this way when there is a stop/pause condition you can just return from the function:

void my_stopwatch(int *superleap, int *leap, int *sec, int *min, int *hour) {
    for(; hour < 24; hour++) {
        for(; min < 60; min++) {
            for(; sec < 60; sec++) {
                for(; leap < 4; leap++) {
                    for(; superleap < 30; superleap++) {
                        system("cls");
                        printf("%02d:%02d:%02d", hour, min, sec);
                        //if ( pause or stop condition) { return ; }
                    }
                    superleap = 0;
                }
                leap = 0;
            }
            sec = 0;
        }
        min = 0;
    }
    hour = 0;
}



So the next problem is with what triggers a pause/stop. Unfortunately the standard libraries do not have anything really helpful here -- to do something like monitoring a keystroke you will need to use a 3rd party library.

If you are on windows then you may have access to kbhit() or __kbhit() which can be used to detect a keystroke. You also have access to the windows API which can do all kinds of neat things with the keyboard.

here is a post where I talk about how to get input under windows

here is a post with a little program demonstrating GetAsyncKeyState under windows

If you are on Linux or another platform you will want to look into curses/ncurses (note if you are on linux you probably already have ncurses installed in the GNU libraries).
Was This Post Helpful? 1
  • +
  • -

#4 Xcody  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 13-August 10

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 07:17 AM

sir. i tried it. but still not working?
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 11:20 AM

define: "tried it"

Show us where you are at now.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 13 August 2010 - 08:58 PM

View PostNickDMax, on 13 August 2010 - 07:20 PM, said:

define: "tried it"


I've tried a simple, basic 'stopwatch' firstly with conio.h, and then with just standard code. It doesn't implement pause, but might give you a couple of ideas.
Note: use of \r instead of system("cls").

The <conio.h> version.
         
#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

/* Get current time */
void mygettime(int *hour, int *min, int *sec)
{
  time_t basetime;
  struct tm * timest;

  time ( &basetime );
  timest = localtime ( &basetime );

  *hour = timest->tm_hour;
  *min  = timest->tm_min;
  *sec  = timest->tm_sec;
}


/* show time as per posters program,
   This function not needed.
   Note: uses \r instead of system("cls")
         uses kbhit() - not standard C */
void showtime(void)
{
  int hour, min, sec;
  int oldsec = 0;

  while(!kbhit())
  {
    mygettime(&hour, &min, &sec);
    if ( sec != oldsec )
      printf("%02d:%02d:%02d\r", hour, min, oldsec = sec);
  }

  printf("\n");
  getch();
}


void printdiff(int hour, int min, int sec)
{
  printf("time difference ...\n");

  if(sec < 0)
  {
    sec = sec + 60;
    min = min -1;
  }

  if(min < 0)
  {
    min = min + 60;
    hour = hour -1;
  }

  printf("%02d:%02d:%02d\n", hour, min, sec);
}


void my_stopwatch(void)
{
  int hour1, min1, sec1;
  int hour2, min2, sec2;

  puts("Press any key to start ...");
  showtime();
  mygettime(&hour1, &min1, &sec1);

  puts("Press any key to stop ...");
  showtime();
  mygettime(&hour2, &min2, &sec2);

  printdiff(hour2-hour1, min2-min1, sec2-sec1);
}


int main()
{
  my_stopwatch();

  getchar();

  return (0);
}



The standard version.
 

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


void mygettime(int *hour, int *min, int *sec)
{
  time_t basetime;
  struct tm * timest;

  time ( &basetime );
  timest = localtime ( &basetime );

  *hour = timest->tm_hour;
  *min  = timest->tm_min;
  *sec  = timest->tm_sec;

}


void showtime(void)
{
  int hour, min, sec;

  mygettime(&hour, &min, &sec);
    
  printf("%02d:%02d:%02d\n", hour, min, sec);
}


void printdiff(int hour, int min, int sec)
{
  puts("time difference ...");

  if(sec < 0)
  {
    sec = sec + 60;
    min = min -1;
  }

  if(min < 0)
  {
    min = min + 60;
    hour = hour -1;
  }

  printf("%02d:%02d:%02d\n", hour, min, sec);

}


void my_stopwatch(void) 
{
  int hour1, min1, sec1;
  int hour2, min2, sec2;
  char buffer[20];

  printf("Press Enter to start ...");
  gets(buffer);
  showtime();
  mygettime(&hour1, &min1, &sec1);

  printf("Press Enter to stop ...");
  gets(buffer);
  showtime();
  mygettime(&hour2, &min2, &sec2);

  printdiff(hour2-hour1, min2-min1, sec2-sec1);
}



int main()
{
  my_stopwatch();

  getchar();

  return (0);

}


Was This Post Helpful? 0
  • +
  • -

#7 Xcody  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 24
  • Joined: 13-August 10

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 05:40 AM

How to put a Pause, Reset and Exit in my stopwatch?

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

int main()
{
int timer, bang, speed, sec, min, hour;

printf("Press 's' to start stopwatch: ");

while(getch() != 's'){
}
for(bang=0; bang<24; bang++)
for(hour=0; hour<60; hour++)
for(min=0; min<60; min++)
for(sec=0; sec<60; sec++)
for(timer=0; timer<100; timer++){
printf("%02d:%02d:%02d\n", hour, min, sec);
system("cls");
}
getch();
return 0;
}
 

This post has been edited by JackOfAllTrades: 16 August 2010 - 06:00 AM
Reason for edit:: Fixed code tags.

Was This Post Helpful? -1
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 06:11 AM

Duplicate topics merged. Please stop duplicate posting.

I don't know what is wrong with the advice everyone gave here, as it seems they put in a lot of effort to help you. And we are certainly not going to do your homework for you. Show some good faith efforts at implementing solutions for your problems and describe your errors.
Was This Post Helpful? 1
  • +
  • -

#9 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 06:44 AM

Maybe my reply was a bit too complicated.


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


int main()
{
 int timer, bang, speed, sec, min, hour;
 char ch = 's';

 printf("Press 's' to start stopwatch: \n");
 while(getch() != 's');

 printf("Press 'p' to pause : \n");
 printf("Press 'r' to reset : \n");
 printf("Press 'e' to exit  : \n");

  while(ch != 'e')
  {
    for(bang=0; bang<24; bang++)
    for(hour=0; hour<60; hour++)
    for(min=0; min<60; min++)
    for(sec=0; sec<60; sec++)
    for(timer=0; timer<100; timer++)
    {
      printf("%02d:%02d:%02d\r",
               hour, min, sec);
      /* system("cls"); */

      if(kbhit() && (ch = getch()) == 'p')
      {
        printf(" pause  \n");
      }
      if(ch == 'r')
      {
        ch = ' ';  
        printf(" reset  \n");
      }
      if(ch == 'e')
      {
        printf(" exit   \n");
        timer = bang = sec = min = hour = 100;
      }
    }
  }

  getch();
  return 0;
}
 




This code is not standard uses conio functions.

This post has been edited by #define: 16 August 2010 - 06:46 AM

Was This Post Helpful? 1
  • +
  • -

#10 leftfingaz  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 38
  • Joined: 28-June 10

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:04 AM

what is ch == ' '?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:08 AM

It compares ch to empty char quotes.
Was This Post Helpful? 1
  • +
  • -

#12 leftfingaz  Icon User is offline

  • New D.I.C Head

Reputation: -9
  • View blog
  • Posts: 38
  • Joined: 28-June 10

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:20 AM

if there is a tutorial for ch? can u give me a site?
so i can learn more about that
Was This Post Helpful? -1
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,367
  • Joined: 27-December 08

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:22 AM

The variable ch is a char variable. Take a look at the introductory C++ tutorials in the C++ tutorials section. Also, let's get back on topic for this thread. If you have problems with your code, or questions regarding C++, you are welcome to open your own thread. :)
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:25 AM

Quote

what is ch == ' '?


On line 36 there is ch = ' ';, only one equal sign, that is assigning a space character to ch.
Could have used another character that isn't r, p or e.

eg. ch = 'x';
Was This Post Helpful? 0
  • +
  • -

#15 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1302
  • View blog
  • Posts: 4,484
  • Joined: 19-February 09

Re: Can anyone help my Stopwatch? C-Programming Only

Posted 16 August 2010 - 07:34 AM

View Postleftfingaz, on 16 August 2010 - 03:20 PM, said:

if there is a tutorial for ch? can u give me a site?
so i can learn more about that



C Tutorial - C Made Easy

If a flash window opens up just close it, to have a look.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3