mercredi 26 mai 2021

Is there a reason for why it doesn't enter some of the else-if statements?

Some of the else-if statements aren't working like the 'else if(strcmp(player1,player2) == 0) ' or the 'else if(strcmp(player1, "scissors") == 0)'. Could it be their subsequent 'if' statements? They are replicas (with some modification) from the other else if statements that do work. They are nearly identical so I don't get why they wouldn't work.

#include <stdio.h>
#include <string.h>

int main(void) {
    
    char player1[25];
    char player2[25];
    
    while(1){
        
        printf("please type one of the following: rock, paper or scissors. Player1 goes first\n");
        scanf("%24s%24s",player1,player2);
        
        if (strcmp(player1, "rock") == 0)
        {
            if (strcmp(player2, "scissors") == 0)
                printf("player 1 wins, rock crushes scissors\n");
        }
        
        else if(strcmp(player1, "rock") == 0)
        {
            if (strcmp(player2, "paper") == 0)
                printf("player 2 wins, paper covers rock\n");
        }

        
        
        else if(strcmp(player1, "scissors") == 0)
        {
            if(strcmp(player2, "paper") == 0)
                printf("player 1 wins, scissor cuts paper\n");
        }
        
        else if(strcmp(player1, "scissors") == 0)
        {
            if(strcmp(player2, "rock") == 0)
                printf("player 2 wins, rock crushes scissors\n");
        }

        
        else if(strcmp(player1, "paper") == 0)
        {
            if(strcmp(player2, "rock") == 0)
                printf("player 1 wins, paper covers rock\n");
        }
        
        else if(strcmp(player1, "paper") == 0)
        {
            if(strcmp(player2, "scissors") == 0)
                printf("player 2 wins, scissor cuts paper\n");
        }

        
        

        else if(strcmp(player2, "rock") == 0)
        {
            if (strcmp(player1, "scissors") == 0)
                printf("player 2 wins, rock crushes scissors\n");
        }
        
        else if(strcmp(player2, "rock") == 0)
        {
            if (strcmp(player1, "paper") == 0)
                printf("player 1 wins, paper covers rock\n");
        }

        
        else if(strcmp(player2, "scissors") == 0)
        {
            if(strcmp(player1, "paper") == 0)
                printf("player 2 wins, scissor cuts paper\n");
        }
        
        else if(strcmp(player2, "scissors") == 0)
        {
            if(strcmp(player1, "rock") == 0)
                printf("player 1 wins, rock crushes scissors\n");
        }

        
        else if(strcmp(player2, "paper") == 0)
        {
            if(strcmp(player1, "rock") == 0)
                printf("player 2 wins, paper covers rock\n");
        }
        
        else if(strcmp(player2, "paper") == 0)
        {
            if(strcmp(player1, "scissors") == 0)
                printf("player 1 wins, scissor cuts paper\n");
        }

        
        else if(strcmp(player1,player2) == 0)
        {
            printf("nobody wins, let’s sing it again!\n");
        }
        else
            printf("incorrect input, try again\n");
    }

        return 0;
}

Aucun commentaire:

Enregistrer un commentaire