Storing GetCh using a pointer

I'm just learning how to use GetCh(). I have the GetCh() call inside a do...while loop and I think I'm having a problem where sometimes the program is not waiting for the user to press a key, instead it is using the same key that was pressed the last time GetCh() was called and continuing on until GetCh() is called again in the next time through the loop.

My actual code is quite long, so I don't want to bore you guys with it, but this is an approximation of what I have:

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
#include <iostream>
#include <stdio.h>

using std::cout;

//Function to get keyboard input
CHAR GetCh (VOID)
{
  HANDLE hStdin = GetStdHandle (STD_INPUT_HANDLE);
  INPUT_RECORD irInputRecord;
  DWORD dwEventsRead;
  CHAR cChar;

  while(ReadConsoleInputA (hStdin, &irInputRecord, 1, &dwEventsRead)) /* Read key press */
    if (irInputRecord.EventType == KEY_EVENT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_SHIFT
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_MENU
	&&irInputRecord.Event.KeyEvent.wVirtualKeyCode != VK_CONTROL)
    {
      cChar = irInputRecord.Event.KeyEvent.uChar.AsciiChar;
	ReadConsoleInputA (hStdin, &irInputRecord , 1, &dwEventsRead); /* Read key release */
	return cChar;
    }
  return EOF;
}

int main()
}
	CHAR KeyPress;
	cout << "To exit, press 'q'\n";
	do
	{	
		cout << "Press a key: ";
		KeyPress = GetCh();
		cout << "\nYou pressed " << KeyPress << "\n";
	}while(KeyPress != 'q')

	return 0;
}


So, something I was thinking of doing to solve the problem, was to create KeyPress as a pointer to CHAR, which would change int main() to:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
}
	CHAR* KeyPress(nullptr);
	cout << "To exit, press 'q'\n";
	do
	{
		KeyPress = nullptr;
		KeyPress = new CHAR;
		cout << "Press a key: ";
		*KeyPress = GetCh();
		cout << "\nYou pressed " << *KeyPress << "\n";
	}while(*KeyPress != 'q')

	delete KeyPress;
	return 0;
}

However, when I use this code, I notice when I'm debugging it that the character located at *KeyPress is the inputted character followed by a series of garbage characters. Though, the code still functions, I believe this is probably sloppy coding. Can anybody help me clean this up or tell me what I'm doing wrong?
You're only missing a small piece. GetCh returns a char, not a string. So why you say as you do in line 9...

Get a char and put it at this particular memory address

that's what you get. The key pressed is stored at that memory address.

The "problem" is on line 10. You are saying...

display the c-style string at this memory address onto the console.

C-style strings are null terminated. So it will continue displaying the characters in subsequent memory addresses until a null character is found.
I don't actually understand any of the code in the CHAR GetCh (VOID) function. I just copied and pasted it from a forum post from somebody suggesting a good way to get keyboard input without echo. Is there a way to adjust this so it is looking for a char instead of a string?
On second look, your code is fine. I ran it through my debugger, and it works with no memory leaks. My comment above is wrong.

I think what you're asking is "is my code sloppy" and the answer is dependent on who is looking at it.

If this is part of a large program that will be maintained for a long while, and your platform supports c++11 style coding, you might consider using auto pointers instead of new/delete.

If this is part of a class assignment where getting input from the keyboard is simply a way to get data into the program, I'd say it works... ship it.

There is a very long list of possibilities as to how to accomplish any task. That's part of why I like C++.
I'm working on the Dungeon Crawl question from http://www.cplusplus.com/forum/articles/12974/
Just for my own amusement and to work on my C++ skills (I'm still a beginner and learning)

It is starting to get large (over 350 lines in int main() )

I'm using the wads keys to move the player around the board. I haven't proven it yet, but I'd swear that sometimes when I press a direction he jumps ahead two spaces instead of one. Is there maybe a way to flush the input buffer before checking for input the next time the loop comes around?


...and your platform supports c++11 style coding


I'm not really sure what that means or how I check. Do you mean what OS I have (i.e. Windows 7) or what compiler I'm using (i.e. Visual Studio 2010)?



Last edited on
I've been doing a bit more testing and now I am definitely getting occurances of the program skipping where it's supposed to wait for keyboard input from the user and instead running through the loop again using the KeyPress value from the previous loop. Any ideas?
Topic archived. No new replies allowed.