Clock

I need my clock to end with 12:00:00 not 12:60:60 i dont know how

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
 #include<iostream>
#include<windows.h>
using namespace std;

void gotoxy( int column, int line )
  {
  COORD coord;
  coord.X = column;
  coord.Y = line;
  SetConsoleCursorPosition(
    GetStdHandle( STD_OUTPUT_HANDLE ),
    coord
    );
  }
  void display()
  {
  	gotoxy(33,8);
cout<<"hr";
gotoxy(36,8);
cout<<"min";
gotoxy(40,8);
cout<<"sec";
  }
int main()
{
	int hours, secs, mins;

	display();
	for (int a=30; a<= 45; a++)
{
	gotoxy(a,6);
	cout<<char(220);
}
	for (int a=45; a>= 30; a--)
{
	gotoxy(a,12);
	cout<<char(220);
}
	for (int a=13; a>= 5; a--)
{
	gotoxy(31,a);
	cout<<char(219);
}
	for (int a=5; a<=13; a++)
{

	gotoxy(44,a);
	cout<<char(219);
}

	for(hours=0; hours <=12; hours++)
{
	gotoxy(34,10);
	cout<<hours<<":";
	for(mins=1; mins<=60; mins++)
{
	gotoxy(37,10);
	cout<<mins<<":";	
	for(secs=1; secs<=60; secs++)
{
	gotoxy(40,10);
	cout<<secs;
	Sleep(.05);
}
}
}
	return 0;
}
closed account (48T7M4Gy)
Two things:

1) The new hour starts one second after 59 minutes and 59 seconds has transpired.
2) Modular arithmetic (%60) and integer division (/) are useful functionalities at your disposal.
3) goto's aren't good practice. PS oops, gotoxy is something else
4) I can't count.
Last edited on
closed account (E0p9LyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<iostream>
#include<windows.h>

void gotoxy(int column, int line)
{
   COORD coord;
   coord.X = column;
   coord.Y = line;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void display()
{
   gotoxy(33, 8);
   std::cout << "hr";

   gotoxy(36, 8);
   std::cout << "min";

   gotoxy(40, 8);
   std::cout << "sec";
}

int main()
{
   int hours, secs, mins;

   display();

   for (int a = 30; a <= 45; a++)
   {
      gotoxy(a, 6);
      std::cout << char(220);
   }

   for (int a = 45; a >= 30; a--)
   {
      gotoxy(a, 12);
      std::cout << char(220);
   }

   for (int a = 13; a >= 5; a--)
   {
      gotoxy(31, a);
      std::cout << char(219);
   }

   for (int a = 5; a <= 13; a++)
   {
      gotoxy(44, a);
      std::cout << char(219);
   }

   for (hours = 1; hours <= 12; hours++)
   {
      gotoxy(34, 10);

      if (hours < 10)
      {
         std::cout << " ";
      }
      std::cout << hours << ":";

      for (mins = 0; mins < 60; mins++) // notice the change
      {
         gotoxy(37, 10);

         if (mins < 10)
         {
            std::cout << "0";
         }
         std::cout << mins <<":";

         for (secs = 0; secs < 60; secs++)  // notice the change
         {
            gotoxy(40, 10);

            if (secs < 10)
            {
               std::cout << "0";
            }
            std::cout << secs;
            Sleep(750); // the Sleep() function is not a good time keeper
         }
      }
   }
}


Your gotoxy() function is fine, it moves the cursor position within the console window. It is Windows specific, though.

kemort is talking about the goto statement I believe.
http://en.cppreference.com/w/cpp/language/goto
Last edited on
closed account (48T7M4Gy)
kemort is talking about the goto statement I believe.

I was, and hopefully my PS a short while ago makes amends for my error.
How gentleguy?
closed account (48T7M4Gy)
we should make it 24 hours clock format

24 hour clock makes sense. And then it becomes an easy choice which clock-type is displayed rather than having an extra PM/AM attribute to be concerned about unnecessarily.

Of course, that doesn't address how it's done other than mention modulo 24.

Nice bit of color. :)
Last edited on
closed account (E0p9LyTq)
To make the clock stop at 12:00:00 requires a bit of logic rework:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include<iostream>
#include<windows.h>

void gotoxy(int column, int line);
void display();

int main()
{
   display();
   
   for (int a = 30; a <= 45; a++)
   {
      gotoxy(a, 6);
      std::cout << char(220);
   }

   for (int a = 45; a >= 30; a--)
   {
      gotoxy(a, 12);
      std::cout << char(220);
   }

   for (int a = 13; a >= 5; a--)
   {
      gotoxy(31, a);
      std::cout << char(219);
   }

   for (int a = 5; a <= 13; a++)
   {
      gotoxy(44, a);
      std::cout << char(219);
   }

   int hours = 1;
   int mins  = 0;
   int secs  = 0;

   while (true)
   {
      // display hours
      gotoxy(34, 10);
      if (hours < 10)
      {
         std::cout << " ";
      }
      std::cout << hours << ":";

      // display minutes
      gotoxy(37, 10);
      if (mins < 10)
      {
         std::cout << "0";
      }
      std::cout << mins <<":";

      // display seconds
      gotoxy(40, 10);

      if (secs < 10)
      {
         std::cout << "0";
      }
      std::cout << secs;
      
      if (hours == 12)
      {
         break;
      }
      
      secs++;
      
      if (secs >= 60)
      {
         mins++;
         secs = 0;
      }
      
      if (mins >= 60)
      {
         hours++;
         mins = 0;
      }

      Sleep(5);
   }
}

void gotoxy(int column, int line)
{
   COORD coord;
   coord.X = column;
   coord.Y = line;
   SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord);
}

void display()
{
   gotoxy(33, 8);
   std::cout << "hr";

   gotoxy(36, 8);
   std::cout << "min";

   gotoxy(40, 8);
   std::cout << "sec";
}


To make the program work as a 24 hr clock simply change line 66 to if (hours == 24). Then the program will stop at 24:00:00.
Last edited on
closed account (E0p9LyTq)
Since the OP's expected output exceeds 12.00, we should make it 24 hours clock format instead.

The OP needs the clock to stop at 12:00:00, not 24:00:00.

Just changing the original for loop logic to work as 24 hr will have the program end at 24:60:60 or 24:59:59.

The digit increment logic has to be revised, as I did, not rewrite the stated output requirement.
closed account (48T7M4Gy)
This shows how the modulo arithmetic might make it a bit easier:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include<iostream>

using namespace std;

void display(const int hh, const int mm, const int ss)
{
    cout<< "Time " << hh << ':' << mm << ':' << ss << '\n';
}

int main()
{
    int hours = 0;
    int mins = 0;
    int secs = 0;
    
    display(hours, mins, secs);
    
    for(int t = 0; t < 3700; t += 20)
    {
        secs = t % 60;
        mins = t/60;
        
        hours = mins / 60;
        mins = mins % 60;
        
        display(hours, mins, secs);
    }
    
    return 0;
}
closed account (48T7M4Gy)
BTW if you want to just stop the clock at any time rather than rolling over to the next 12 or 24 cycle the if(hours >= 12 || hours >= 24) will do the trick.
Thankyou so much everyone :)
Topic archived. No new replies allowed.