0
#include <stdio.h>

int main(int argc, char* argv[])
{
    char inp1, inp2, inp3;
    int LOOP1 = 1,LOOP2 = 1, LOOP3 = 1;
    
    while (LOOP1 == 1)
    {
        printf("Player-1 it is your turn!\nPlease enter your choice (p)aper, (r)ock, or (s)cissors: ");
        
        while (LOOP2 == 1)
        {
            scanf("%c", &inp1);
            if (inp1 != 'p' || (inp1!='r'|| inp1!='s'))
            {
                printf("I'm sorry, I do not understand.\nPlease enter your choice (p)aper, (r)ock, or (s)cissors: ");
            }
            if (inp1 == 'p' || (inp1=='r' || inp1=='s'))
            {
                LOOP2=0;
            }
        }
        
        printf("Player-2 it is your turn!\nPlease enter your choice (p)aper, (r)ock, or (s)cissors: ");
        
        while (LOOP3 == 1)
        {
            scanf("%c", &inp2);
            if (inp2 != 'p' || (inp2!='r'|| inp2!='s'))
            {
                printf("I'm sorry, I do not understand.\nPlease enter your choice (p)aper, (r)ock, or (s)cissors: ");
            }
            if (inp2 == 'p' || (inp2=='r' || inp2=='s'))
            {
                LOOP3=0;
            }
        }
        
        if (inp2 == 'p')
        {
            if (inp1 == 'p')
                printf("No one wins.\n");
            if (inp1 == 'r')
                printf("Player-2 wins! Paper covers rock.\n");
            if (inp1 == 's')
                printf("Player-1 wins! Scissors cuts paper.\n");
        }
        if (inp2 == 'r')
        {
            if (inp1 == 'p')
                printf("Player-1 wins! Paper covers rock.\n");
            if (inp1 == 'r')
                printf("No one wins.\n");
            if (inp1 == 's')
                printf("Player-2 wins! Rock breaks scissors.\n");
        }
        if (inp2 == 's')
        {
            if (inp1 == 'p')
                printf("Player-2 wins! Scissors cuts paper.\n");
            if (inp1 == 'r')
                printf("Player-1 wins! Rock breaks scissors.\n");
            if (inp1 == 's')
                printf("No one wins.\n");
        }
        
        printf("Do you wish to continue? (y/n): ");
        scanf("%c", &inp3);
        if (inp3 == 'n')
            LOOP1 = 0;
    }

    return 0;
}

When you run the program, the following lines will be printed out: Player-1 it is your turn! Please enter your choice (p)aper (r)ock (s)cissors:

For instance, if I input the character "p" (as in "paper"), this is when my bug occurs. I would expect that it would then print out these lines: Player-2 it is your turn! Please enter your choice (p)aper (r)ock (s)cissors:

However, it prints out these lines: I'm sorry, I do not understand. Please enter your choice (p)aper, (r)ock, or (s)cissors: Player-2 it is your turn! Please enter your choice (p)aper, (r)ock, or (s)cissors: I'm sorry, I do not understand. Please enter your choice (p)aper, (r)ock, or (s)cissors:

Let's say that despite that bug, I type in the character 's'. I would expect the following output after giving scanf the input 's': Player-2 wins! Scissors cuts paper. Do you wish to continue? (y/n):

However, this is what is actually output: I'm sorry, I do not understand. Please enter your choice (p)aper, (r)ock, or (s)cissors: Player-2 wins! Scissors cuts paper. Do you wish to continue? (y/n): Player-1 it is your turn! Please enter your choice (p)aper, (r)ock, or (s)cissors: Player-2 it is your turn! Please enter your choice (p)aper, (r)ock, or (s)cissors: Player-2 wins! Scissors cuts paper. Do you wish to continue? (y/n):

help

  • `scanf("%c", &inp1);` -> `scanf(" %c", &inp1);` Put a space before `%c` to skip leading whitespace, like the newline from the enter key on the previous input. – Retired Ninja Oct 26 '22 at 01:23
  • Note: drop the `LOOP2`, `LOOP3` and use `while(1)` and `break`. Also, use `else` rather than (incorrectly) negating the `if` expression. – Ken Y-N Oct 26 '22 at 01:30

0 Answers0