vendredi 12 juillet 2019

How to fix space in character that lead to being printed twice in a loop? (C)

I am doing a mini RPG text-based adventure just for fun. Now you need to choose a gender, and that's where my problem happens.

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)){
        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 the program loop until you either say male or female (please don't be offended). That part is fine. However if you type something with a space (say "attack helicopter"), it will print something like:

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?

Basically it will print the first part once then loop the second part once too.

I haven't used Stack Overflow in a long time, and I am a complete beginner in C so please excuse me if I'm not doing something properly.

Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire