gpt4 book ai didi

c - 为什么我的最终 scanf 不停止并读取用户输入?

转载 作者:行者123 更新时间:2023-11-30 15:49:45 36 4
gpt4 key购买 nike

任何人都可以告诉我为什么我的代码工作正常,直到我到达最后的不足,我询问用户是否想再次玩?由于某种原因,程序似乎忽略了这行代码。请温柔一点,因为我是编程新手,正在尝试自学 Objective-C。这个程序对于菜鸟来说是典型的,我生成一个随机数,要求用户猜测,然后询问他们是否想再玩一次。谢谢。

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[])
{

@autoreleasepool {

int randomNumber = arc4random_uniform(100); // This is a random number generator that gens a num betw 0 and 100
int userNumber; // This is the number that the user picks intially
int attempts = 0; // This is the number of attempts the user makes during each game
int games = 0; // This is the number of games the user has played
char play = 'n'; // This is whether the user wants to play again, intially set to 'y'

scanf("%c", &play);
while (play == 'y') {

NSLog(@"Random number is: %d", randomNumber);
NSLog(@"Enter a number between 0 and 100");
scanf("%d", &userNumber);
games++; // Increment the number of games the user has played to 1

if (userNumber == randomNumber) {
attempts++;
NSLog(@"Congratulations. You guessed correctly!");
}

attempts++;

while (userNumber != randomNumber) {

if (userNumber < randomNumber) { // Guess is too low
attempts++; // attempt is incremented
NSLog(@"Too low. Try again!"); // User tries again
scanf("%d", &userNumber);
}

if (userNumber > randomNumber) { // Guess is too high
attempts++; // attempt is incremented
NSLog(@"Too high. Try again!"); // User tries again
scanf("%d", &userNumber);
}
}
NSLog(@"Congratulations. You guessed correctly!");
NSLog(@"It took you %d attempts to guess correctly", attempts);


NSLog(@"Do you want to play again?");
scanf("%c", &play); // --------- Here is where things to wrong ---------

} // while play is yes

} // autoreleasepool
return 0;
} // main

最佳答案

将评论转换为答案:

Probably, the final scanf() reads a newline and continues (the numbers don't read the newline). Maybe put a blank before the %c:

scanf(" %c", &play);

Check the return value from scanf(), and maybe even check which character was read.

反击:

That space before the %c did the trick. How does anyone ever learn things like that? I think it was reading the \n char rather than what I wanted it to read, which was either 'y' or 'n'. For my understanding, the %d integer doesn't read in the newline, but the %c does? Is that correct? And the way to prevent this is to use a space? I just don't get how I would ever know to do that.

响应:

By reading the manual page for scanf() very carefully, many times over, or by bitter experience (or by answering lots of questions on SO about it). The scanf() family of functions are extremely powerful and extremely difficult to use accurately. I generally recommend using fgets() to read lines of input:

char line[4096];
if (fgets(line, sizeof(line), stdin) != 0)
{
...use line...
}

combined with sscanf() to parse the data on the line. It generally leads to fewer surprises of the sort you just ran into. You should always check that scanf() made as many conversions as you expected.

scanf()系列格式字符串中空格的作用非常复杂。大多数转换说明符会自动跳过前导空格(包括换行符),因此格式字符串 "%d%d" 将读取整数值,其中第一个值前面可能有任意数量的空格,第二个前面也可以有任意数量的空格。转换将在第一个不能属于第二个整数的字符处停止(除非之前出现错误)。如果您键入 8 和换行符作为输入,则转换会在换行符 (\n) 处停止,并留下 if 以供下一个输入读取。

数字转换和字符串转换%s都会跳过前导空格。单字符格式 (%c) 和扫描集 %[a-z] 不会跳过前导空格。

当格​​式中出现空白字符时,如“%d %c”,则它表示数据中任意数量的空白,包括零。因此,在以下每一行中,接收 %c 格式的变量每次都会获得 Z:

123Z
123 Z
123 Z
123
Z

(最后两行一起读取以获取最后一个输入。)

关于c - 为什么我的最终 scanf 不停止并读取用户输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16125423/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com