Scanf 在 C 中每隔一个 while 循环跳过 [英] Scanf skips every other while loop in C

查看:24
本文介绍了Scanf 在 C 中每隔一个 while 循环跳过的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个简单的基于文本的刽子手游戏,主游戏循环从提示输入每个字母的猜测开始,然后继续检查该字母是否在单词中并杀死如果不是,请关闭.但是,当我运行游戏时,每次都会出现两次提示,并且程序不会等待用户的输入.它还带走了一条生命(如果输入正确则为一条生命,如果不正确则为两条生命),因此无论它输入的是什么都与之前的输入不同.这是我的游戏循环,简化了一点:

I'm trying to develop a simple text-based hangman game, and the main game loop starts with a prompt to enter a guess at each letter, then goes on to check if the letter is in the word and takes a life off if it isn't. However, when I run the game the prompt comes up twice each time, and the program doesn't wait for the user's input. It also takes off a life (one life if it was the right input, two if it wasn't), so whatever it's taking in isn't the same as the previous input. Here's my game loop, simplified a bit:

while (!finished)
{
    printf("Guess the word '%s'
",covered);

    scanf("%c", &currentGuess);

    i=0;
    while (i<=wordLength)
    {
        if (i == wordLength)
        {
            --numLives;
            printf("Number of lives: %i
", numLives);
            break;
        } else if (currentGuess == secretWord[i]) {
            covered[i] = secretWord[i];
            secretWord[i] = '*';
            break;
        }
        ++i;
    }

    j=0;
    while (j<=wordLength)
    {
        if (j == (wordLength)) {
            finished = 1;
            printf("Congratulations! You guessed the word!
");
            break;
        } else {
            if (covered[j] == '-') {
                break;
            }
        }
        ++j;

        if (numLives == 0) {
            finished = 1;
        }

    }
}

我认为问题是 scanf 认为它已将某些东西带入但我不知道为什么.有谁有想法吗?我在 Mac OS X 10.5 上使用 gcc 4.0.1.

I assume the problem is scanf thinking it's taken something in when it hasn't, but I have no idea why. Does anyone have any idea? I'm using gcc 4.0.1 on Mac OS X 10.5.

推荐答案

当你用 scanf() 读取键盘输入时,输入是在按下回车后读取但回车键产生的换行符不会被 scanf() 的调用所消耗.这意味着下次您从标准输入读取时,会有一个换行符等着您(这将使下一个 scanf() 调用立即返回而没有数据).

When you read keyboard input with scanf(), the input is read after enter is pressed but the newline generated by the enter key is not consumed by the call to scanf(). That means the next time you read from standard input there will be a newline waiting for you (which will make the next scanf() call return instantly with no data).

为避免这种情况,您可以将代码修改为:

To avoid this, you can modify your code to something like:

scanf("%c%*c", &currentGuess);

%*c 匹配单个字符,但星号表示该字符不会存储在任何地方.这会消耗由回车键生成的换行符,以便下次调用 scanf() 时,您将从一个空的输入缓冲区开始.

The %*c matches a single character, but the asterisk indicates that the character will not be stored anywhere. This has the effect of consuming the newline character generated by the enter key so that the next time you call scanf() you are starting with an empty input buffer.

警告:如果用户按下两个键然后按回车键,scanf() 将返回第一次击键,吃掉第二次,并为下一次留下换行符输入调用.像这样的怪癖是许多程序员避免 scanf() 和朋友的原因之一.

Caveat: If the user presses two keys and then presses enter, scanf() will return the first keystroke, eat the second, and leave the newline for the next input call. Quirks like this are one reason why scanf() and friends are avoided by many programmers.

这篇关于Scanf 在 C 中每隔一个 while 循环跳过的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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