scanf函数跳过所有其他,而在空调回路 [英] Scanf skips every other while loop in C

查看:83
本文介绍了scanf函数跳过所有其他,而在空调回路的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想开发一个简单的基于文本的刽子手游戏,游戏主循环一个提示开始于每个字母输入一个猜测,然后继续检查字母在单词,并采取生活关闭,如果事实并非如此。然而,当我运行游戏的提示两次,每次出现时,程序不等待用户的输入。它还起飞生活(一命,如果它是正确的输入,两路如果不是),所以无论它采取的是不一样的previous输入。这里是我的游戏循环,简化了一点:

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'\n",covered);

	scanf("%c", &currentGuess);

	i=0;
	while (i<=wordLength)
	{
		if (i == wordLength)
		{
			--numLives;
			printf("Number of lives: %i\n", 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!\n");
			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()的,输入被读入后pressed而是由输入密钥生成新行没有被调用消耗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).

要避免这种情况,可以修改code到是这样的:

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.

警告:如果用户presses两个键,然后presses进入, 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函数跳过所有其他,而在空调回路的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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