【问题标题】:elementary C issue初级C问题
【发布时间】:2016-10-06 17:43:55
【问题描述】:

所以我希望你不能输入诸如 0 或 11 之类的数字。我使用的是netbeans 8.1 (GNU cygwin)。我发现没有使用 while 循环,并且终端运行成功构建,而不管为确保不存在超出范围的数字而进行的循环。

int main(int argc, char** argv) {
int x;
int guess = 0;
srand(time(NULL));

x = rand() % (10) + 1;

printf("random number is %d. \n", x);

printf("whats your guess? 3 tries. between 1 and 10 \n");
scanf("%d", guess);

while (guess >10 || guess < 0){
    printf(" your guess is out of the bounds (1 to 10) re enter: \n");
    scanf("%d", guess);
}

return (EXIT_SUCCESS);

}

有什么想法吗? 我打算稍后与“x”进行几次比较。

【问题讨论】:

  • scanf() 调用返回的值是多少?你不是在测试失败。
  • 你需要在guess前面加一个&号...scanf("%d", &amp;guess);
  • 如果您不能使用调试器,您至少可以在其中添加一些打印语句来提供帮助。我建议在几个地方添加fprintf(stderr, "guess = %d\n", guess);
  • 我通过在 printf 中返回guess 的值来测试它。谢谢yano,忘了&。

标签: c while-loop scanf


【解决方案1】:

在 scan f 语句中只需要一个 &(和号)。

   scanf("%d", &guess);

【讨论】:

    猜你喜欢
    • 2015-11-16
    • 1970-01-01
    • 1970-01-01
    • 2011-09-27
    • 1970-01-01
    • 1970-01-01
    • 2010-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多