简单的“按键”程序
我刚刚开始使用 C++,正在尝试找出如何检测按下的键。我知道“a”的 ASCII 代码是 97。所以...
1) 以下程序有什么问题?
#include <iostream>
using namespace std;
int main()
{
char thing[1];
cin >> thing;
if (thing == 97)
cout << "You pressed 'a'!\n";
return 0;
}
2)我怎样才能做到这一点,而不是必须键入字母作为输入并按回车键,程序只需立即接受按键?
I'm just starting out in C++, and am trying to work out how to detec what key has been pressed. I understand the ASCII code for 'a' is 97. So...
1) What is wrong with the following program?
#include <iostream>
using namespace std;
int main()
{
char thing[1];
cin >> thing;
if (thing == 97)
cout << "You pressed 'a'!\n";
return 0;
}
2) How can I make it so that rather than having to type the letter as an input and press enter, the program just accepts the keypress immediately?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
欢迎!我知道您会发现学习 C++ 编程既令人困惑又充满乐趣。我可以建议你先买一本好的 C++ 书吗?这里有一个很好的此类书籍列表:权威 C++ 书籍指南和列表< /a>
您不应导入整个
std
命名空间。该命名空间中有很多符号,您几乎肯定会在某个时刻与其中一个符号发生冲突。我知道许多初学者教科书都会指导您这样做。不。没有理由将
thing
声明为数组。它应该是char thing;
。因为,当您这样做时,就会产生缓冲区溢出错误。由于
thing
是一个数组,cin
会将thing
视为 C 风格字符串,并愉快地将整个输入行写入其中,即使它不适合。thing
的定义),thing==97
将数组与常量 97 进行比较。更具体地说,它比较第一个元素的地址数组的值变为常量 97。这是一件坏事,甚至可能无法编译。thing
的定义),赤裸裸的常量97
会让代码的读者(包括您自己)感到困惑。相反,将thing
与同等有效的整数常量'a'
进行比较。把它们放在一起:
正如其他人所指出的,您必须为此使用特定于平台的 API。在 Microsoft Windows 上,尝试
getch()
。Welcome! I know you will find learning to program C++ both confusing and enjoyable. May I suggest that you first acquire a good C++ book? There is an excellent list of such books here: The Definitive C++ Book Guide and List
You shouldn't import the entire
std
namespace. There are a lot of symbols in that namespace, and you will almost certainly collide with one of them at some point. I know that many beginner textbooks instruct you to do this. Don't.There is no reason to declare
thing
an array. It should bechar thing;
.Because, when you do this, you create a buffer-overflow bug. Since
thing
is an array,cin
will treatthing
as a C-style string, and happily write the entire input line to it, even if it doesn't fit.thing
),thing==97
compares the array to the constant 97. More specifically, it compares the address of the first element of the array to the constant 97. This is a bad thing, and probably won't even compile.thing
), the naked constant97
is confusing to the readers of your code, including to yourself. Instead comparething
to the equally valid integral constant'a'
.Putting it all together:
As others have pointed out, you must use a platform-specific API for this. On Microsoft Windows, try
getch()
.1)
如果他们在按 Enter 之前按多个字符,则不安全,因为您只为 1 个字符分配了空间。
另外,你想要
2)没有标准的方法。函数
getch()
适用于某些平台。1)
is unsafe if they press more than one character before pushing enter, as you've only allocated space for 1 character.
Also, you want
2) There is no standard way. The function
getch()
works on some platforms.使用函数
_getch()
可以给你一个字符,而无需等待回车键。只需包含 conio.h 并使用它即可。它可以在 Windows 上运行,但在 Visual C++ 中被列为已弃用。所以这是一种非标准方式并且不可移植。
如果你想返回按下的按键的字符代码并将按下的字符输出到控制台,那么你可以使用
getche_()
Use the function
_getch()
to give you a character without waiting for the enter key. Just include conio.h and use it.It works on Windows but it is listed as deprecated for visual C++. So it is a non standard way and not portable.
If you want to return the character code of the key pressed and output the character pressed to the console then you can use
getche_()