C++:获取空格字符后的字符或回车

发布于 2024-10-19 12:55:20 字数 1105 浏览 1 评论 0原文

好吧,这与我的上一个问题类似,但我最终所做的对于像这样简单的事情来说太复杂了。我只需要在按空格键后从控制台获取单个字符或数字(我会知道我收到的是哪一个),而不是按 Enter 键。我确信一定有一种方法可以让终端在空格而不是“\n”之后读取输入。我需要从控制台读取输入,其中后续的数据类型将根据第一个输入的不同而变化,我认为读取整行,将其解析为字符串,然后将其中一些解析为整数是有点不必要的。

那么这在 C++ 中实际上是不可能的还是我只是还没有找到它?

编辑:

对于遇到此问题的任何人,我都会发布我的解决方案,因为我现在感觉像个白痴。

#include <iostream>

using namespace std;

int main() {

    int command = 0, x = 0, y = 0, z = 0;
    char c;

    do {
        cin >> command;
        switch(command) {
            case 1:
                cin >> c >> x;
                cout << c << " " << x << endl;
                break;
            case 2:
                cin >> x >> y >> z;
                    cout << x << " " << y << " " << z << endl;
                    break;
        }
    } while (command); //Exits when command = 0;

    return 0;

}

switch 语句中的以下 cin 将从与第一个 cin 相同的缓冲区中读取,因此无需事先读取命令的内容。正如您所看到的,这对于第一个 cin 之后的不同类型和数量的输入都适用,因此无需使用任何其他解决方案。

只是将这个发布给可能有同样问题的其他人,并且对 cin 的工作方式没有很好的理解。

Okay this is similar to my last question but what I ended up doing was way too complex for something as simple as this. I simply need to get a single character or number (I will know which of these I am receiving) from the console after I press space, instead of pressing enter. I'm sure there must be a way to have the terminal read input after a space instead of a '\n'. I need to read inputs from the console in which the succeeding data types will vary depending on what the first input is, and I think reading the entire line, parsing it into strings, then parsing some of those into ints is a bit unnecessary.

So Is this actually not possible in C++ or have I just not found it yet?

EDIT:

For anyone who has had this problem I'm posting my solution, because I feel like an idiot now.

#include <iostream>

using namespace std;

int main() {

    int command = 0, x = 0, y = 0, z = 0;
    char c;

    do {
        cin >> command;
        switch(command) {
            case 1:
                cin >> c >> x;
                cout << c << " " << x << endl;
                break;
            case 2:
                cin >> x >> y >> z;
                    cout << x << " " << y << " " << z << endl;
                    break;
        }
    } while (command); //Exits when command = 0;

    return 0;

}

The following cin's inside the switch statement will read from the same buffer as the first cin, so there is no need to read what the command is beforehand anyway. As you can see this works fine for different types and amounts of inputs after the first cin, so there's no need to use any other solution.

Just posting this for anyone else who may have the same problem, and not a great understanding of the way cin works.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

画尸师 2024-10-26 12:55:20

只需在循环中多次调用 getchar() (或 getch()),并在每次迭代时检查输入。

像这样的东西(未经测试但应该有效):

int loop = 1
int spacehit = 0
char last_c = 0;
while(loop)
{
    char c = getchar();
    switch(c)
    {
        case ' ':
            spacehit = 1;
            printf("hit '%c' before 'space'!\n", last_c);
            break;
        case 'x':
            printf("hit 'x' after 'space'!\n");
            spacehit = 0;
            break;
        case 27: // escape
            loop = 0;
            break;
        default:
            // do something, e.g. append the key in c to a string
            break;
    }
    last_c = c;
}

编辑:添加了一些代码以在点击空格之前打印字符,以防万一这就是您正在寻找的内容。

Just issue multiple calls to getchar() (or getch()) in a loop and check the input every iteration.

Something like this (untested but should work):

int loop = 1
int spacehit = 0
char last_c = 0;
while(loop)
{
    char c = getchar();
    switch(c)
    {
        case ' ':
            spacehit = 1;
            printf("hit '%c' before 'space'!\n", last_c);
            break;
        case 'x':
            printf("hit 'x' after 'space'!\n");
            spacehit = 0;
            break;
        case 27: // escape
            loop = 0;
            break;
        default:
            // do something, e.g. append the key in c to a string
            break;
    }
    last_c = c;
}

Edit: Added some code to print the char before hitting space in case that's what you were looking for.

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