Check for input while program still running

I have a program that displays a clock on screen, but I would like to be able to press the 'M' key, and the time will show military time, press it a second time, and it'll revert back to standard time. Every time I have tried, the seconds do not count upwards, unless I keep a finger depressing a key. How can I add this function, without stalling the clock? Here is the program. Hopefully, someone can show me how to add it.
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
// LCD Clock.cpp : Defines the entry point for the console application.

#include <stdafx.h>  // For Microsoft Express. Delete if using different one.
#include <ctime>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void gotoXY(int x, int y );
void gotoXY(int x, int y, string text);
void setcursor(bool visible, DWORD size);

int main()
{
	setcursor(0,0);
	int hours, minutes, seconds, t;
	struct tm * timeinfo;
	
	string AM_PM;

	string Segment[11][10]={
		{" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
		{"\x1E     \x1E","      \x1E","      \x1E","      \x1E","\x1E     \x1E","\x1E      ","\x1E      ","      \x1E","\x1E     \x1E","\x1E     \x1E"},
		{"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
		{"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
		{"\x1F     \x1F","      \x1F","      \x1F","      \x1F","\x1F     \x1F","\x1F      ","\x1F      ","      \x1F","\x1F     \x1F","\x1F     \x1F"},
		{"       ","       ", " \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
		{"\x1E     \x1E","      \x1E","\x1E      ","      \x1E","      \x1E","      \x1E","\x1E     \x1E","      \x1E","\x1E     \x1E","      \x1E"},
		{"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
		{"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
		{"\x1F     \x1F","      \x1F","\x1F      ","      \x1F","      \x1F","      \x1F","\x1F     \x1F","      \x1F","\x1F     \x1F","      \x1F"},
		{" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "}
	};
	string Colon[11][1] = {
		{"       "},
		{"       "},
		{"       "},
		{"   \xDB   "},
		{"       "},
		{"       "},
		{"       "},
		{"   \xDB   "},
		{"       "},
		{"       "},
		{"       "}
	};
	
	do
	{
	time_t rawtime;
	time ( &rawtime );
	timeinfo = localtime( &rawtime );
	hours = timeinfo->tm_hour;
	seconds = timeinfo->tm_sec;
	minutes = timeinfo->tm_min;
	AM_PM = "AM";
	if ( hours >= 12)
		AM_PM = "PM";
	if ( hours == 0 )
		hours = 12;
	if ( hours > 12)
		hours -=12;
	for(t=0;t<11;t++)
	{
		gotoXY(8,7+t);
		cout  << Segment[t][hours/10] << "   " << Segment[t][hours%10] << Colon[t][0] << Segment[t][minutes/10] << "   " << Segment[t][minutes%10] << Colon[t][0] << Segment[t][seconds/10] << "   " << Segment[t][seconds%10];
	}
	gotoXY(75,17, AM_PM);
	} while (true);
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}

void gotoXY(int x, int y, string text) 
{ 

	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
	cout << text;
}

void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
{
	if(size == 0)
	{
		size = 20;	// default cursor size
	}
	CONSOLE_CURSOR_INFO lpCursor;	
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console,&lpCursor);
}


This is NOT, nor ever was, a school project, assignment or homework. I'm not even IN school. Thanks for any help, or insight, my fellow programmers may extend.

EDIT: Didn't like some of the numbers. Looked at a picture of some LCD numbers, and changed mine to look a bit more like those.
Last edited on
Do I understand correctly:

Do you want to read keyboard inout between do while (true) loop, ie. betw lines 54 and 74?

If so, can you use the [_kbhit()] function to see if there was keyboard input.

If so, you read it and check if it is an 'M' - then do your thing.

will this piece of code help you?
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
#include <iostream>
#include <conio.h>
#include <windows.h>


using namespace std;



void main()
{
   while (true)
   {
      cout << "processing, processing, processing - start ..." << endl;

      if (_kbhit())
      {
         char ch = getch();

         if (ch == 0)
         {
            cout << "A special key was pressed like F3 ..." << endl;
            ch = getch();     //special keys have two keyboard input codes --> we sink the next one
         }
         else if (ch == 'x' || ch == 'X')   
         {
            cout << "You pressed 'x' - we are exiting now!!!" << endl;
            break;
         }
      }

      Sleep(1000);

      cout << "processing, processing, processing - end" << endl << endl;
   }
}
@SIK

Thank you very much. That was EXACTLY what I needed. I added
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
if (_kbhit())
      {
         char ch = getch();
         ch = toupper(ch);
         if (ch == 'M')      
         {
           Military_Time = !Military_Time;
         }
      }
		if( Military_Time)
		{
			AM_PM = "  ";
		}
		else
		{
		AM_PM = "AM";
		if ( hours >= 12)
			AM_PM = "PM";
		if ( hours == 0 )
			hours = 12;
		if ( hours > 12)
			hours -=12; 
		}
in place of lines 61 through 67, and it works like a charm. My thanks again.
You're welcome - really glad to have been able to help a fellow c++ programmer.

Just would like to point out something if you don't mind.

You might notice your cpu usage for your program being very high when you open your taskmanager.

The main reason for this will be due to the tight while (true) loop. If you count the number of iterations within this loop per second you would probably notice it to be very high. If you don't require it to be so high, then you can decrease your cpu usage substantially by adding a simple Sleep within while(true) loop without loss of functionality to overall program logic.
@SIK

I never mind learning new things. Thanks. I added in the Sleep(1000) into the program. I hadn't really given any thought to what was happening while the program ran.

I hope I see something you may have a problem with, that I can be helpful, for you. In the mean time, Merry Christmas and a joyous New Year Year to you and your family..
Here is a version of "kbhit" that will reduce your loop load on Windows:
http://www.cplusplus.com/forum/beginner/5619/#msg25047
Last edited on
I agree with Duoas - this will give you more control on windows.

@whitenite1: Thankyou for the kind gesture and I wish you and your family a blessed christmas and joyous new year too.
@Duoas

The code looks interesting, but I have no idea how to implement it. Where in my code, in the OP would I put it, and what code would it replace?
@Duoas

I put
1
2
3
4
while ( !iskeypressed(500 ))
    {
		cout << "." << flush;
    }
between minutes = timeinfo->tm_min; and if (_kbhit()) section ( that was put in using SIK's suggestion ) and the iskeypressed function after main, and I didn't replace anything. The program is working, so I must have set it up correctly. Thanks for all your help and guidance, Duoas and SIK
Last edited on
This is on window .. what if i want to run this program in linux .
@whitenite1
Did I not say that iskeypressed() is a replacement for _kbhit()?

@bluecoder
The OP is using Windows.
But, just because you asked, this is how you do it on *nix:
http://www.cplusplus.com/forum/beginner/11271/#msg53251

[edit] Oh, I forgot to mention that for the *nix version, you should #define INFINITE -1 ...[/edit]

:-)
Last edited on
@Duoas

Actually, you only mentioned it was
a version of
, so I didn't really think to replace it. So, my other question still remains.
The code looks interesting, but I have no idea how to implement it. Where in my code, in the OP would I put it, and what code would it replace?

Here is the complete program, as far as I have gone. I removed the iskeypressed section, since I had it setup wrong. Would appreciate it, letting me know what gets changed. Thanks..

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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
// LCD Clock.cpp : Defines the entry point for the console application.

#include <stdafx.h>  // For Microsoft Express. Delete if using different one.
#include <ctime>
#include <conio.h>
#include <iostream>
#include <string>
#include <windows.h>

using namespace std;

HANDLE console = GetStdHandle(STD_OUTPUT_HANDLE);
COORD CursorPosition;

void Draw(int style, int col, int row, int length,int amount, bool fill, int sw );
void gotoXY(int x, int y );
void gotoXY(int x, int y, string text);
void setcursor(bool visible, DWORD size);

int main()
{
	setcursor(0,0);
	int hours, minutes, seconds, t;
	struct tm * timeinfo;
	bool Military_Time = false;

	string AM_PM;

	string Segment[11][10]={
		{" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
		{"\x1E     \x1E","      \x1E","      \x1E","      \x1E","\x1E     \x1E","\x1E      ","\x1E      ","      \x1E","\x1E     \x1E","\x1E     \x1E"},
		{"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
		{"\xDB     \xDB","      \xDB","      \xDB","      \xDB","\xDB     \xDB","\xDB      ","\xDB      ","      \xDB","\xDB     \xDB","\xDB     \xDB"},
		{"\x1F     \x1F","      \x1F","      \x1F","      \x1F","\x1F     \x1F","\x1F      ","\x1F      ","      \x1F","\x1F     \x1F","\x1F     \x1F"},
		{"       ","       ", " \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "},
		{"\x1E     \x1E","      \x1E","\x1E      ","      \x1E","      \x1E","      \x1E","\x1E     \x1E","      \x1E","\x1E     \x1E","      \x1E"},
		{"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
		{"\xDB     \xDB","      \xDB","\xDB      ","      \xDB","      \xDB","      \xDB","\xDB     \xDB","      \xDB","\xDB     \xDB","      \xDB"},
		{"\x1F     \x1F","      \x1F","\x1F      ","      \x1F","      \x1F","      \x1F","\x1F     \x1F","      \x1F","\x1F     \x1F","      \x1F"},
		{" \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 ","       "," \x11\xDB\xDB\xDB\x10 "," \x11\xDB\xDB\xDB\x10 "}
	};
	string Colon[11][1] = {
		{"       "},
		{"       "},
		{"       "},
		{"   \xFE   "},
		{"       "},
		{"       "},
		{"       "},
		{"   \xFE   "},
		{"       "},
		{"       "},
		{"       "}
	};
	Draw(2,2,5,76,15,0,2);
	gotoXY(26,12,"Pressing the \"M\" key");
	gotoXY(26,14,"toggles Military Time");
	Sleep(2000);
	Draw(1,4,6,72,13,1,0);
	
	gotoXY(73,8,"HR");
	do
	{
		time_t rawtime;
		time ( &rawtime );
		timeinfo = localtime( &rawtime );
		hours = timeinfo->tm_hour;
		seconds = timeinfo->tm_sec;
		minutes = timeinfo->tm_min;

		if (_kbhit())
      {
         char ch = _getch();
		 ch = toupper(ch);
         if (ch == 'M')   
         {
           Military_Time = !Military_Time;
		   ch = ' ';
         }
      }
		if( Military_Time)
		{
			AM_PM = "  ";
			gotoXY(73,7,"24");
		}
		else
		{
		AM_PM = "AM";
		gotoXY(73,7,"12");
		if ( hours >= 12)
			AM_PM = "PM";
		if ( hours == 0 )
			hours = 12;
		if ( hours > 12)
			hours -=12;
		}

		for(t=0;t<11;t++)
		{
			gotoXY(6,7+t);
			cout  << Segment[t][hours/10] << "   " << Segment[t][hours%10] << Colon[t][0] << Segment[t][minutes/10] << "   " << Segment[t][minutes%10] << Colon[t][0] << Segment[t][seconds/10] << "   " << Segment[t][seconds%10];
	    }
		
		gotoXY(73,17, AM_PM);
		Sleep(1000);
	} while (true);
}

void Draw(int style, int col, int row, int length,int amount, bool fill, int sw )
{
	// Draws a 1 or 2 line box 
	int a;
	if ( sw >4)
		sw = 4;
	style=(style-1)*6;
	char box[24];
	char shdw[5];

	box[0] = '\xDA';//  ┌  Draw(1,...);
	box[1] = '\xBF';//  ┐
	box[2] = '\xC0';//  └
	box[3] = '\xD9';//  ┘
	box[4] = '\xB3';//  │ 
	box[5] = '\xC4';//  ─
	box[6] = '\xC9';//  ╔  Draw(2,...); 
	box[7] = '\xBB';//  ╗
	box[8] = '\xC8';//  ╚
	box[9] = '\xBC';//  ╝
	box[10] = '\xBA';// ║
	box[11] = '\xCD';// ═
	box[12] = '\xD5';//  ╒  Draw(3,...);
	box[13] = '\xB8';//  ╕
	box[14] = '\xD4';//  ╘
	box[15] = '\xBE';//  ╛
	box[16] = '\xB3';//  │ 
	box[17] = '\xCD';//  ═
	box[18] = '\xD6';//  ╓  Draw(4,...);
	box[19] = '\xB7';//  ╖
	box[20] = '\xD3';//  ╙
	box[21] = '\xBD';//  ╜
	box[22] = '\xBA';// ║
	box[23] = '\xC4';// ─
	shdw[1] = '\xB0';// ░  
	shdw[2] = '\xB1';// ▒
	shdw[3] = '\xB2';// ▓
	shdw[4] = '\xDB';// █
	char tl,tr,bl,br,side,edge,shadow;
	tl = box[style];
	tr = box[style+1];
	bl = box[style+2];
	br = box[style+3];
	side = box[style+4];
	edge = box[style+5];
	shadow = shdw[sw];
	string Line(length-2,edge);
	string Shadow(length,shadow);
	string Fill(length-2, ' ');
	gotoXY(col,row);
	cout << tl << Line << tr;
	for (a = 1; a <amount-1;a++)
	{
		gotoXY(col,row+a);
		cout << side;
		if  (fill)
			cout << Fill;
		else		
			gotoXY(col+length-1,row+a);
		cout << side;
		if (sw)
			cout << shadow;
	}
	gotoXY(col,(amount+row)-1);
	cout << bl << Line << br;
	if (sw)
	{
		cout << shadow;
		gotoXY(col+1,row+amount , Shadow );
	}
}

void gotoXY(int x, int y) 
{ 
	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition); 
}

void gotoXY(int x, int y, string text) 
{ 

	CursorPosition.X = x; 
	CursorPosition.Y = y; 
	SetConsoleCursorPosition(console,CursorPosition);
	cout << text;
}

void setcursor(bool visible, DWORD size) // set bool visible = 0 - invisible, bool visible = 1 - visible
{
	if(size == 0)
	{
		size = 20;	// default cursor size
	}
	CONSOLE_CURSOR_INFO lpCursor;	
	lpCursor.bVisible = visible;
	lpCursor.dwSize = size;
	SetConsoleCursorInfo(console,&lpCursor);
}
Last edited on
Topic archived. No new replies allowed.