简体   繁体   English

如果用户没有输入任何内容或输入错误,如何重新输入用户的输入?

[英]How to re-input a user's input in case he didn't input anything or inputted wrong?

I'm working on a mastermind game as a university project.我正在做一个策划游戏作为一个大学项目。 The user has to guess a number of colors with a certain order.用户必须以特定顺序猜测多种颜色。 The problem is if the user inputs something like "erd" instead of "red" his chance would be lost.问题是如果用户输入诸如“erd”而不是“red”之类的东西,他的机会就会丢失。 I wanted to make the code somehow that it would say "invalid color!"我想以某种方式使代码显示“无效颜色!” when he inputs something invalid and let him re-enter his thing.当他输入无效的东西并让他重新输入他的东西时。 I kinda did it but there is a small problem, look at the code below:我有点做到了,但有一个小问题,请看下面的代码:

void guess(char guessBall[4][10]) {
  for (int i = 0; i < 4; i++) {
    cin >> guessBall[i];
    if (strcmp(guessBall[i], "red") == 0 || strcmp(guessBall[i], "blue") == 0 ||
        strcmp(guessBall[i], "green") == 0 ||
        strcmp(guessBall[i], "yellow") == 0 ||
        strcmp(guessBall[i], "white") == 0 ||
        strcmp(guessBall[i], "black") == 0) {
      continue;
    } else {
      cout << "invalid color!";
      i--;
    }
  }
}

The problem is that if the user inputs like this: red blue WRONG black instead of pressing enter each time, it would say "invalid color!"问题是,如果用户像这样输入: red blue WRONG black而不是每次都按 Enter,它会说“无效颜色!” and let him re-enter, but the order of the colors would be wrong.让他重新进入,但颜色的顺序是错误的。 example:例子:

INPUT: "red WRONG green blue"
OUTPUT: Invalid Color!
INPUT: "Red"
OUTPUT: Your guess: red green blue red

the guess should be "red red green blue".猜测应该是“红红绿蓝”。

Would be thankful if u guys gave any suggestions.如果你们提供任何建议,将不胜感激。 Full code as requested:EDIT: I seemingly am unable to paste the code on my post, some parts of it get messed up and i don't know what to do.要求的完整代码:编辑:我似乎无法将代码粘贴到我的帖子中,其中某些部分搞砸了,我不知道该怎么办。 The only thing I could think of was pasting the code on a CodePile page.我唯一能想到的就是将代码粘贴到 CodePile 页面上。 here u go: https://www.codepile.net/pile/4rYRMJ93 i think codepile is a safe place ?你去吧: https ://www.codepile.net/pile/4rYRMJ93 我认为 codepile 是一个安全的地方?

The cin will take the inputs in the order you type them. cin将按照您键入的顺序接收输入。 Your input example is "red WRONG green blue red".您的输入示例是“红色错误绿色蓝色红色”。 My suggestion is that you use the debugger and put a break point a continue and one in the else statement and you can see what is going on.我的建议是您使用调试器并在else语句中放置一个断点continue和一个断点,您可以看到发生了什么。

My suggestion would be to make one part that handle the input and one part validating the answer.我的建议是让一部分处理输入,另一部分验证答案。 Then you can call the input function until the validation is correct.然后就可以调用输入函数,直到验证正确为止。

The cin will processes the input in the order it was typed in. Since "red WRONG green blue" is typed in before the second "red," this second red is placed at the end of the input stream. cin 将按照输入的顺序处理输入。由于“red WRONG green blue”是在第二个“red”之前输入的,因此第二个红色被放置在输入流的末尾。 Your code is probably fine, just type colors one at a time when testing your program.您的代码可能没问题,只需在测试程序时一次键入一种颜色。

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

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