如何修复导致循环打印两次的字符空间? (C)

时间:2019-07-12 07:27:10

标签: c for-loop if-statement char printf

我正在做一个迷你RPG基于文本的冒险,只是为了好玩。现在您需要选择一种性别,这就是我的问题所在。

for(;; ){
    printf("Are you a male or a female?\n");
    scanf ("%s", &gender);
    if ((strcmp(gender, "female")==0) || (strcmp(gender, "Female")==0) || (strcmp(gender, "Girl")==0) || (strcmp(gender, "girl")==0) || (strcmp(gender, "male")==0) || (strcmp(gender, "Male")==0) || (strcmp(gender, "Boy")==0) || (strcmp(gender, "boy")==0)){
        printf("What's your name? (Limited to 20 chars)");
        break;
    }
    else {
        printf("So you are non-binary? %s doesn't exist in our database.\n", gender);
    }
}

因此,程序将循环运行,直到您说(是男性)还是(女性)为止。那部分很好。但是,如果您输入带有空格的内容(例如“攻击直升机”),它将显示以下内容:

So you are non-binary? attack doesn't exist in our database.
Are you a male or a female?
So you are non-binary? helicopter doesn't exist in our database.
Are you a male or a female?

基本上它将先打印第一部分,然后再循环第二部分。

0 个答案:

没有答案