Home > Net >  My statement inside else if under line 41 to 51 is not being executed
My statement inside else if under line 41 to 51 is not being executed

Time:10-11

I am writing a program using c that stimulates a game called rock, paper, and scissors.

The rest of the script works perfectly but the else if statement in line 41-51 is not being excuted.

int user;
int robot;

printf ("Rock, Paper, and Scissors \n\n");

printf("Rock = 1, Paper = 2, Scissors = 3\n\n");

printf("User's move: ");
scanf("%d", &user);
printf("Robot's move: ");
scanf("%d", &robot);

if(user == 1|| robot == 2)
{
    printf("Robot wins by choosing Paper!");
}
else if(user == 2 || robot == 1)
{
 printf("User wins by choosing Paper!");
}
else if (user == 2 || robot == 3)
{
    printf("Robot wins by choosing Scissors! ");
}
else if (user == 3 || robot == 2)
{
    printf("User wins by choosing Scissors!");
}
else if (user == 3 || robot == 1)
{
    printf("Robot wins by choosing Rock!");
}
else if (user == 1 || robot == 3)
{
    printf("User wins by choosing Rock!");
}
else if (user == 1 || robot == 1)
{
    printf("It's a tie!");
}
else if (user == 2 || robot == 2)
{
    printf("It's a tie!");
}
else if (user == 3 || robot == 3)
{
    printf("It's a tie!");
} }

It should print the message "its a tie" whenever I input the same number in users input but it wasn't executing.

CodePudding user response:

|| means "or". E.g. user == 1 || robot == 2 is checking to see if either user == 1 or robot == 2. Either of those will result in printf("Robot wins by choosing Paper!"); being executed.

Once you get to the tie conditions, every possibility (given the user entering 1, 2, or 3) has occurred. None of the tie messages can be printed.

If you use && for "and" your code will behave the way you expect as you will know that both conditions are true.

Also note, that for a tie condition, you could write either:

else if (user == robot) {
    ...
}

Or assuming you've validated that user and robot are both either 1, 2, or 3, and you've covered all other possible combinations, simply:

else {
    ...
}

CodePudding user response:

Getting Boolean logic right can be tough at the beginning.
You could write the code without using explicit boolean symbols (or tons of curly braces):

if( user == robot)
    printf("It's a tie!");
else if(user == 1)
    if( robot == 2)
        printf("Robot wins by choosing Paper!");
    else
        printf("User wins by choosing Rock!");
else if(user == 2)
    if(robot == 1)
        printf("User wins by choosing Paper!");
    else
        printf("Robot wins by choosing Scissors! ");
else if(user == 3)
    if(robot == 2)
        printf("User wins by choosing Scissors!");
    else
        printf("Robot wins by choosing Rock!");

EDIT:
With the first of the DVs recorded, I accept the challenge...

Here is Sheldon Cooper's "Rock Paper Scissors Lizard Spock". Maybe someone will take the time to write the && and || version to compare. I don't know what that might look like; it'd be interesting to see. (This uses rand() to generate the opponent's moves.)

char *dr = "DRAW\n\n";

char *sp = "scissors cuts paper";
char *pr = "paper covers rock";
char *rl = "rock crushes lizard";
char *ln = "lizard poisons Spock";
char *ns = "Spock smashes scissors";
char *sl = "scissors decapitates lizard";
char *lp = "lizard eats paper";
char *pn = "paper disproves Spock";
char *nr = "Spock vaporizes rock";
char *rs = "rock crushes scissors";

char *tbl[][6] = {
    { dr, pr, rs, rl, nr, " lwwl", },
    { pr, dr, sp, lp, pn, "w llw", },
    { rs, sp, dr, sl, ns, "lw wl", },
    { rl, lp, sl, dr, ln, "lwl w", },
    { nr, pn, ns, ln, dr, "wlwl ", },
};

void evaluate( int usr, int cpu ) {
    char *elems[] = { "rock", "paper", "scissors", "lizard", "nimoy(spock)" };
    printf ( "\nYou: %-14s - PC: %-14s - %s", elems[ usr ], elems[ cpu ], tbl[usr][cpu] );
    if( usr != cpu )
        printf( " - %s\n\n", tbl[usr][5][cpu] == 'w' ? "WIN!!" : "lose..." );
}

void oneRound() {
    char *choices = "rpslnRPSLN";
    char buf[32];

    printf( "choose [r]ock [p]aper [s]cissors [l]izard [n]imoy(Spock): " );
    fgets( buf, sizeof buf, stdin );
    char *cp = strchr( choices, buf[0] );
    if( cp == NULL )
        puts( "bad entry" );
    else
        evaluate( ( cp - choices ) % 5, rand() % 5 );
}

int main() {
    srand( time(NULL) );
    for( ;; )
        oneRound();

    return 0;
}

EDIT2:
There are many ways to skin those cats. Here's RPS with NO explicit boolean operations and only 3 pairs of curly braces inside the single function block.

/* The usual headers */
int main() {
    srand( time(NULL) );

    char *elems[] = { "rock", "paper", "scissors" };
    char *choices = "rpsRPS";
    char buf[32];

    for( ;; ) {
        printf( "choose rock paper scissors: " );
        fgets( buf, sizeof buf, stdin );
        char *cp = strchr( choices, buf[0] );
        if( cp == NULL ) { puts( "bad entry" ); continue; }
        int usr = ( cp - choices ) % 3;
        int cpu = rand() % 3;
        printf ( "You chose: %s - PC chose: %s - ", elems[ usr ], elems[ cpu ] );

        if( usr == cpu )
            puts( "Draw" );
        else
            puts( ( usr   1 ) %3 == cpu ? "Lose" : "Win" );
    }

    return 0;
}
  •  Tags:  
  • c
  • Related