Aug 24, 2016 at 2:33am Aug 24, 2016 at 2:33am UTC
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 Aug 24, 2016 at 2:57am Aug 24, 2016 at 2:57am UTC
Aug 24, 2016 at 3:03am Aug 24, 2016 at 3:03am UTC
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 Aug 24, 2016 at 3:04am Aug 24, 2016 at 3:04am UTC
Aug 24, 2016 at 3:38am Aug 24, 2016 at 3:38am UTC
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 Aug 24, 2016 at 3:39am Aug 24, 2016 at 3:39am UTC
Aug 24, 2016 at 3:46am Aug 24, 2016 at 3:46am UTC
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 Aug 24, 2016 at 3:51am Aug 24, 2016 at 3:51am UTC
Aug 24, 2016 at 4:15am Aug 24, 2016 at 4:15am UTC
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.
Aug 24, 2016 at 4:42am Aug 24, 2016 at 4:42am UTC
Thankyou so much everyone :)