c - 为什么我的第一个 if 语句被跳过并且不接受 C 中的输入?

标签 c macos if-statement sleep scanf

代码:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
int input;
system("clear");
printf("Welcome to Riddles! A game of mystery. Press any key and the enter button to continue.\n");
scanf("%d", &input);

system("clear");
sleep(1);
printf("Riddle 1. At night they come without being told. By day they are gone without being stolen. What are they?\n");
printf("1. goats\n");
printf("2. pillows\n");
printf("3. memories\n");
printf("4. the stars\n");
scanf("%d", input);

if (input == 1)
{
    system("clear");
    sleep(1);
    printf("Correct. The stars is the answer\n");
}

if (input != 1)
{
    sleep(1);
    system("clear");
    printf("Incorrect Game over\n");
}

}

问题:输出将打印:Welcome to Riddles!一场神秘的游戏。按任意键和回车按钮继续”就像应该的那样,但是当我输入字母并按回车键时,会出现以下内容:

谜语 1. 晚上他们不经通知就来了。白天它们就消失了,没有被偷。这些是什么? 1. 山羊 2.枕头 3.记忆 4.星星

游戏结束错误

问题是,我什至无法输入答案。它将跳到第二个 if 语句而不接受任何输入。有谁知道问题出在哪里吗?

(附:我从《指环王北方 war 》中得到了这个谜语)

最佳答案

此声明中有一个拼写错误

scanf("%d", input);

应当

scanf("%d", &input);

此外,您不能为 int 类型的对象输入字母。因此,更改第一个 scanf 以便您可以输入字母。

关于c - 为什么我的第一个 if 语句被跳过并且不接受 C 中的输入?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26832537/

相关文章:

c - 为什么输入96时会出现段错误?

linux - 信息索引(emacs 内和外)

macos - 当大多数应用程序 'documents' 不是基于文件时,NSDocument 是正确的选择吗?

Python Pandas 条件标记

c - gcc:降低 libc 所需的版本

C套接字从接受返回的文件描述符中获取IP地址

C 拼图 {MACRO}

iphone - GCD、线程、程序流程和 UI 更新

java - 使用Java检查变量是否在两个数字之间

matlab - MATLAB 中的级联 if/elseif/else 构造是否有简洁的替代方案?