只输入某些字符 [英] input only certain characters

查看:92
本文介绍了只输入某些字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在c ++中创建程序以仅允许输入某些字符? (请尽可能简单)

例如,如果用户输入了一个句子,但程序只读取某些字母

How do you make the program in c++ to allow to input only certain characters? (as simple as possible please)
For example, if the user has input a sentence but the program only reads certain alphabets

推荐答案

如果你想简单地忽略一些角色事件然后使用标准的C / C ++库,据我所知你不能这样做。您需要一些特定于平台的输入方法,只需为您检索输入,而无需将其实际打印到屏幕上。然后你可以检查它,如果你喜欢这个角色,那么你将它放入缓冲区并将其打印到屏幕上。由于您只指定了没有任何平台的C ++,因此您需要搜索这样一个特定于平台的输入方法(无需打印即可检索该字符)。



我这样做了仅在带有汇编和bios中断的DOS上。还有一个带有getch()函数的conio.h头文件可能对您有用。这是一个测试程序,您可以使用它来查找此函数的功能。我把它作为一个教训让你找出如何忽略字符以及如何将有用字符收集到缓冲区中。

If you want to simply ignore some character events then with the standard C/C++ library you can not do this as far as I know. You need some platform specific input method that just retrieves the input for you without actually printing it out to the screen. Then you can check it and if you like the character then you put it into your buffer and print it to screen. Since you specified only C++ without any platform then its up to you to search for such a platform specific input method (that retrieves the character without printing).

I did this only on DOS with assembly and bios interrupts. There is also a conio.h header with a getch() function that may be useful for your purposes. Here is a test program that you can use to find out what this function does. I leave it as a lesson for you to find out how to ignore characters and how to collect the useful ones into a buffer.
#include <conio.h>
#include <stdio.h>

int main()
{
    for (;;)
    {
        int ch = getch();
        printf("%d\n", ch);
    }
    return 0;
}





编辑:嗯,没有那么简单的部分可以实现,所以我提供了一个你可以使用的基本实现作为基础:



Well, there are not so trivial parts to implement so I provide a basic implementation that you can use as a base:

#include <conio.h>
#include <stdio.h>

// buf_size includes the terminating zero so the max string len is buf_size-1
bool TestFilteredInput(char* buf, size_t buf_size)
{
    if (!buf_size)
        return false;
    static const char ALLOWED_CHARS[] = "abcdefABCDEF0123456789";
    char* p = buf;
    char* q = buf + buf_size - 1;

    for (;;)
    {
        int c = getch();
        // 8: backspace
        // 13: enter
        // 27: esc
        if (c == 27)
            return false;

        if (c == 13)
        {
            *p = 0;
            printf("\n");
            return true;
        }

        if (c == 8)
        {
            if (p == buf)
            {
                // there is no char to delete
                // \a is bell, chr(7)
                printf("\a");
            }
            else
            {
                // \b is backspace, chr(8). Printing it moves the cursor to the left by one char
                // move cursor left, write space to "overwrite/erase" the last char then move cursor left again
                printf("\b \b");
                --p;
            }
        }
        else if (p >= q)
        {
            // max string len reached
            printf("\a");
        }
        else if (strchr(ALLOWED_CHARS, c))
        {
            *p++ = (char)c;
            printf("%c", c);
        }
        else
        {
            // invalid char
            printf("\a");
        }
    }
}

int main()
{
    char buf[5];
    bool res = TestFilteredInput(buf, sizeof(buf));
    printf("retval: %d\n", res?1:0);
    if (res)
        printf("buf: %s\n", buf);
    return 0;
}





编辑:当你接受一个角色时,你可以将任何你想要的东西打印到屏幕上,这样你就可以实现例如,通过仅打印'*'字符来输入密码...

您可以检查缓冲区的当前状态,如果缓冲区不包含有效输入或您想要的所有内容,则可以忽略输入,当输入一个字符时,你可以进行任意操作/检查(比如打印字符串而不是字符,检查字符是否是附加到缓冲区当前内容的有效字符,...)。



When you accept a character you can print whatever you want to the screen, this way you can implement for example password input by printing only '*' characters...
You can check the current state of the buffer and you can ignore enter if the buffer doesn't contain a valid input or everything you want, when a character is entered you can do arbitrary actions/checking (like printing a string instead of a character, checking whether the character is a valid character to append to the current contents of the buffer, ...).


请参阅

- 过滤掉无效的用户输入 [ ^ ]

- 如何调用来自字符串的字符 [ ^ ]



干杯,

Edo
See
- Filtering out invalid user inputs[^]
- How to remove characters from a string[^]

Cheers,
Edo


你需要过滤程序中的输入。将所有文本读取到字符串并删除不允许的内容
you need to filter the input in the programm. Read all text to a string and remove the not allowed stuff


这篇关于只输入某些字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆