1

我希望能够将一个字符stdin与我的规范字符进行比较。这样做的目的是过滤掉所有其他错误的输入,同时只保留指定的单个字符作为命令。就像在stdin"nn" 或 "qddaw" 上一样 -> 再次出错,但 "n" 使一些有用的东西。

这是我想到的“代码方面”:

if (input does not contain 'c' or 's' or 'q' or 'n') { 
    printf("some kind of error");
}

好吧,我尝试创建一个具有指定字符的数组,array[] = {'a', 'b', 'c'}这样我就可以使用函数 strncmp.. 将它与标准输入上的字符串进行比较

char c[256];
scanf("%s", c)
if (strncmp(array, c, 1) != 0) printf("error");

但它似乎不起作用。有什么建议么?

Edit1:这是实际的一段代码:

char c[256];
char* s = "nsrld";
char* quiter = "q";

do 
    {
        printf(">");
        scanf("%s", c);
        if (only when there is no 'n' or 's' or other char from char* s on input)
        {
            errorHandle(ERROR_WRONG_CMD);
        }
        scanf("%*[^\n]"); scanf("%*c");
    } while (strcmp(c,quiter) != 0);

如您所见,我很好地处理了“q”的事情,但是多个字符很麻烦。感谢您的任何建议。

编辑2:或者换句话说,我需要一个函数,它将输入与一组给定的字符进行比较,并且只有当有一个另一个时(如'q'或's'函数才会通过(但如果有字符在一起则不会)像'qs')

4

8 回答 8

1
int ch = getchar();
if (ch != EOF)
{
    if (strchr("csqn", ch) == NULL)
    {
        printf("Evil Bad Character <%c> in Hex %02X\n", ch, ch);
    }
    else
    {
        /* Do stuff with ch */
    }
}
else
{
    printf("EOF on input\n");
}
于 2012-04-14T23:46:12.337 回答
1
char c = getchar();
switch (c) {
case 'c':
case 's':
case 'q':
case 'n':
  do_something();
  break;
default:
  print_error();
};

上面的代码应该可以工作。我不知道为什么你的 if 语句不起作用。一般来说,开关在这种情况下也能很好地工作。

于 2012-04-14T22:54:06.383 回答
1

我说得不够清楚。我需要的是像“wwqwqe”一样输入“输入你想要的任何东西”并执行错误,除非输入只是'c'或只是's'(以及更多)。

char usersInput[200] = "";            /* A buffer to hold the input values */
char *result = gets(usersInput);      /* Fill the buffer from stdin */
if (result != NULL)                   /* If we got something */
{
    if (strlen(usersInput) == 1)      /* the input must be exactly 1 character */
    {
        char ch = usersInput[0];
        if (strchr(ch, "csqn") == NULL)    /* It must be a valid values */
        {
            printf("Evil Bad Character <%c>\n", ch);
        }
        else
        {
            /* Do stuff with the known valid input value ch */
        }
    }
    else
    {
        puts("The input value must be exactly 1 character\n");
        puts("and must be 'c', 's', 'q' or 'n'");
    }
}
else
{
    puts("EOF or ERROR while reading stdin\n");
}

这应该可以完成这项工作。

一警告。 get不够聪明,无法知道usersInput的长度为 200 个字符。

它会让您兴高采烈地输入 201 个或更多字符,从而覆盖内存中的其他字符。这类事情可能会导致难以发现的错误。

于 2012-04-15T13:04:35.873 回答
0

我认为字符串是集合...因此,如果它们的交集是 void 集合,那么我们将失败-> printf("Error")... 否则输出为无...

#include <stdio.h>
#include <string.h>

int intersection(char* source, char* search)
{
  int i,j;
  for(i = 0; i < strlen(search); i++)
    if(strchr(source,search[i]))j++;
  if(j != strlen(search))return 0;
  else return 1;
}

int main()
{
  char *letters = "eo";
  char *p = "hello";
  int e = intersection(p,letters);
  if(e==1)puts("Non Void");  
  else puts("Void");
}
于 2012-04-14T23:50:22.263 回答
0

写一个函数

int IsGood(int c)
{
    if(c=='a'||c=='b'||c=='c')
    return 1;
    else
    return 0;
}
于 2012-04-14T22:55:54.807 回答
0

您的第一个解决方案应该有效。如果这与您发布的代码完全相同 - 那么您的问题可能是因为 printf 最后需要一个换行符才能刷新到控制台。

于 2012-04-14T22:58:05.977 回答
0

这对我有用:

    char c[256];
    char* s = "nqsrld";
    char* quiter = "q";

    do 
    {
        printf(">");
        scanf("%s", c);
        if ((strpbrk(s, c) == 0) || (strlen(c) >= 2))
        {
            errorHandle(ERROR_WRONG_CMD);
        }
        scanf("%*[^\n]"); scanf("%*c");
    } while (strcmp(c,quiter) != 0);

感谢大家的帮助。

于 2012-04-15T12:44:50.323 回答
0

虽然看起来您已经找到了解决方案,但值得一提的是,您所要求的听起来似乎与标准的“getopt”功能相去甚远……见http://例如www.gnu.org/software/libc/manual/html_node/Getopt.html 。

于 2012-04-15T12:59:04.483 回答