-1

我无法让输入/输出正常工作。请帮忙。

这是我的代码...

    char choice1;

    printf("This is a text game! You will be shown what is going on");
    printf("\nand it is up to you to decide what to do.");

    printf("\n\nThere is a gem on the ground.");
    printf("\nWhat do you want to do");
    printf("\n>");


    scanf("%c", &choice1);

    if (choice1 == pick up gem) {
        printf("Got Gem");
    }
4

1 回答 1

3

%c用于输入单个字符,而不是字符串。如果您想允许用户输入多个字符,那么您需要类似以下内容:

char string[256];
fgets(string, 255, stdin);

if (strcmp(string, "pick up gem\n") == 0) {
    printf("Got Gem");
}

顺便说一句 - 这不是 Objective-C,这是 C。

如果用户输入超过 256 个字符,就会发生不好的事情。

更新:原来scanf只抓取输入的第一个单词。使用fgets通过换行符向上读取。

于 2013-02-27T22:56:07.163 回答