简单的“按键”程序

发布于 2024-11-08 10:50:58 字数 328 浏览 0 评论 0原文

我刚刚开始使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

眼泪都笑了 2024-11-15 10:50:58

我刚刚开始接触 C++

欢迎!我知道您会发现学习 C++ 编程既令人困惑又充满乐趣。我可以建议你先买一本好的 C++ 书吗?这里有一个很好的此类书籍列表:权威 C++ 书籍指南和列表< /a>

1)下面的程序有什么问题?

#include <iostream>
using namespace std;

您不应导入整个 std 命名空间。该命名空间中有很多符号,您几乎肯定会在某个时刻与其中一个符号发生冲突。我知道许多初学者教科书都会指导您这样做。不。

int main()
{
  char thing[1];

没有理由将 thing 声明为数组。它应该是char thing;

  cin >> thing;

因为,当您这样做时,就会产生缓冲区溢出错误。由于 thing 是一个数组,cin 会将 thing 视为 C 风格字符串,并愉快地将整个输入行写入其中,即使它不适合。

if (thing == 97)
  cout << "You pressed 'a'!\n";
  • (假设您没有修复 thing 的定义),thing==97 将数组与常量 97 进行比较。更具体地说,它比较第一个元素的地址数组的值变为常量 97。这是一件坏事,甚至可能无法编译。
  • (假设您修复了 thing 的定义),赤裸裸的常量 97 会让代码的读者(包括您自己)感到困惑。相反,将 thing 与同等有效的整数常量 'a' 进行比较。

把它们放在一起:

#include <iostream>
using std::cout;
using std::cin;

int main()
{
  char thing;

  cin >> thing;
  if (thing == 'a')
    cout << "You pressed 'a'!\n";
  else
    cout << "You pressed not-'a': " << thing << "\n";
  return 0;
}

2)如何才能使程序立即接受按键,而不是必须键入字母作为输入并按 Enter 键?

正如其他人所指出的,您必须为此使用特定于平台的 API。在 Microsoft Windows 上,尝试 getch()

I'm just starting out in C++

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

1) What is wrong with the following program?

#include <iostream>
using namespace std;

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.

int main()
{
  char thing[1];

There is no reason to declare thing an array. It should be char thing;.

  cin >> thing;

Because, when you do this, you create a buffer-overflow bug. Since thing is an array, cin will treat thing as a C-style string, and happily write the entire input line to it, even if it doesn't fit.

if (thing == 97)
  cout << "You pressed 'a'!\n";
  • (Assuming your didn't fix the definition of 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.
  • (Assuming you fixed the definition of thing), the naked constant 97 is confusing to the readers of your code, including to yourself. Instead compare thing to the equally valid integral constant 'a'.

Putting it all together:

#include <iostream>
using std::cout;
using std::cin;

int main()
{
  char thing;

  cin >> thing;
  if (thing == 'a')
    cout << "You pressed 'a'!\n";
  else
    cout << "You pressed not-'a': " << thing << "\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?

As others have pointed out, you must use a platform-specific API for this. On Microsoft Windows, try getch().

oО清风挽发oО 2024-11-15 10:50:58

1)

cin >> thing

如果他们在按 Enter 之前按多个字符,则不安全,因为您只为 1 个字符分配了空间。

另外,你想要

if(thing[0] == 97) // ...

2)没有标准的方法。函数getch()适用于某些平台。

1)

cin >> thing

is unsafe if they press more than one character before pushing enter, as you've only allocated space for 1 character.

Also, you want

if(thing[0] == 97) // ...

2) There is no standard way. The function getch() works on some platforms.

幸福还没到 2024-11-15 10:50:58

使用函数_getch() 可以给你一个字符,而无需等待回车键。只需包含 conio.h 并使用它即可。

它可以在 Windows 上运行,但在 Visual C++ 中被列为已弃用。所以这是一种非标准方式并且不可移植。

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int character;

    character = getch_();
    if (character == 97)
        cout << "You pressed 'a'!\n";
    return 0;
}

如果你想返回按下的按键的字符代码并将按下的字符输出到控制台,那么你可以使用 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.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int character;

    character = getch_();
    if (character == 97)
        cout << "You pressed 'a'!\n";
    return 0;
}

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_()

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文