简体   繁体   English

验证特定字符的用户输入

[英]Validating User Input for Specific Char

I'm creating a program that runs a card game, I need to get a trump suit (H, D, S, C) and validate user input. 我正在创建一个运行纸牌游戏的程序,我需要获得王牌服装(H,D,S,C)并验证用户输入。 I am able to catch invalid stuff like j or 3, but if they enter HH, or SS (which is invalid) it thinks it's correct. 我可以捕获j或3之类的无效内容,但是如果它们输入HH或SS(无效),则认为它是正确的。

I've tried using strlen to see if there's more chars, and I thought maybe I could check if the buffer has other garbage in it, but have no idea how to do that. 我试过使用strlen看看是否还有更多的字符,我想也许我可以检查一下缓冲区中是否还有其他垃圾,但是不知道该怎么做。 I also tried doing something like while(scanf(" %c", &trump) != 1 , but that didn't work. I've only been coding in c for a week. 我也尝试做类似while(scanf(" %c", &trump) != 1事情,但这while(scanf(" %c", &trump) != 1 ,我只用c编写了一个星期。

my tump function 我的翻车功能

char getTrump(){
  char trump;
  char ch;

  char correct = 1;
  while(correct == 1){
    printf("Please choose a trump suit (H, D, S, C): ");
    scanf(" %c", &trump);
    while(getchar() != '\n');

    if(isValidSuit(trump) == 1){
      printf("Correct\n");
      return trump;
      correct = 0;
    }else{
      printf("Please enter a valid suit.\n");
    }
  }
}

and if it helps my isValidSuit function (which has to have a char as a parameter) 以及是否有帮助我的isValidSuit函数(必须具有char作为参数)

int isValidSuit(char c){
  char suits[] = {'H', 'D', 'S', 'C'};

  for(int i = 0; i < 4; i++){
    if(suits[i] == c){
      return 1;
    }
  }

  return 0;
}

an example of running it: 运行它的一个例子:

Please choose a trump suit (H, D, S, C): j
Please enter a valid suit.
Please choose a trump suit (H, D, S, C): HH
Correct

if they enter HH, or SS (which is invalid) it thinks it's correct. 如果他们输入HH或SS(无效),则认为它是正确的。

Of course because when you give input HH , the scanf(" %c", &trump); 当然,因为当您输入HHscanf(" %c", &trump); read first H of input in trump variable and left the second H of input in the input buffer. 读取trump变量中输入的第一个H ,然后将第二个输入H保留在输入缓冲区中。 After this, the getchar() in while loop reads the leftover user input. 之后, while循环中的getchar()读取剩余的用户输入。 The isValidSuit() finds H as value of trump variable and returns 1 . isValidSuit()找到H作为trump变量的值,并返回1 Hence, you are getting Correct for HH or SS input. 因此,您将获得HHSS输入Correct的信息。

To fix this problem, you can check if the input if more than 1 character and that character is not newline character than prompt user to enter valid input, like this: 要解决此问题,您可以检查输入是否超过1字符并且该字符不是换行符,而不是提示用户输入有效输入,例如:

char getTrump(){
    char trump;
    char ch;

    char correct = 1;
    while(correct == 1){
        printf("Please choose a trump suit (H, D, S, C): ");
        scanf(" %c", &trump);
        if (getchar() != '\n') {
            while(getchar() != '\n');
            printf("Please enter a valid suit.\n");
            continue;
        }
    .......
    .......

I think you would be better off (generally speaking anyway) using fgets for the user input. 我认为使用fgets作为用户输入会更好(总而言之)。 scanf is problematic by comparision. scanf的比较存在问题。 fgets reads input in this case from stdin (the keyboard). 在这种情况下,fgets从stdin(键盘)读取输入。 Now it's simple to remove any newline character and test the length. 现在,很容易删除任何换行符并测试长度。

char getTrump(){
  const int suitSize = 12;
  char suit[suitSize];
  bool correct = false;
  memset(suit, 0, suitSize);

  do
  {
    printf("Please choose a trump suit (H, D, S, C): ");
    fgets(suit, suitSize-1, stdin);

    if (suit[strlen(suit)-1] == '\n')
        suit[strlen(suit)-1] = '\0';

    // check input length only one char
    if (strlen(suit) == 1 && isValidSuit(*suit) == 1){
        printf("Correct\n");
        correct = true;
    }
    else{
        printf("Please enter a valid suit.\n");
    }
  } while (!correct);
  return *suit;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM