-1

Im coding a Rock Paper Scissors program and eveything works completely fine except for one thing. In the line of the else condition, the one that contains the while loop (choice != 1 || choice != 2 || choice != 3) with the finishing break; command, it only ever works when an inncorect input like 4 is entered once but not a second time.

When an incorrect input is entered the first time the program will ask for the input again but if you enter another inncorrect input it will just continously just keep printing the same printf line

printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n"); and then proceed to just keep printing

Computer choice is ROCK. Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.

  while (counter != 10) {

    for (i=0;i<1;i++) {
      /* Generate a random number and restrict it to the range 1 to 3 */
      randNum = rand()%3+1;
      /*printf("A random number between 1 and 3: %d\n", randNum);*/
    }

    printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");

    scanf("%d", &choice);


    /* User picks an option */
    
      if ( choice == 1 ){
        printf("User choice is ROCK.\n");
      }
      else if ( choice == 2 ){
        printf("User choice is PAPER.\n");
      }
      else if ( choice == 3 ){
        printf("User choice is SCISSORS.\n");
      }  
      else {
        while (choice != 1 || choice != 2 || choice != 3) {
    
          printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
          scanf("%d", &choice);
          break;
      }

    }

The output when I enter an incorrect input twice

Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
4
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.
4
Computer choice is ROCK.
Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.

I've already tried using a break command but even that doesnt fix the issue. What do I have to change in my program to fix this issue?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Does this answer your question? [How do I save a Winforms panel's drawing content to a file?](https://stackoverflow.com/questions/26936109/how-do-i-save-a-winforms-panels-drawing-content-to-a-file) – TaW Nov 01 '20 at 07:17
  • `choice != 1 || choice != 2` is always true. – chux - Reinstate Monica Nov 01 '20 at 14:15

2 Answers2

1

The condition choice != 1 || choice != 2 || choice != 3 will always be true. For example (as you have noticed), if choice is 4, then it will not equal 1, so the first test will be true and, thus (as you are combining tests with a logical OR), the whole condition will also be true.

However, the condition will also be true when choice is 1. Because, although the first test (choice != 1) will then be false, the second test (choice != 2) will now be true (as 1 is not equal to 2). Further, there is no possible number for which all three tests (or, indeed, any two of them) can be false.

What you want, instead of the OR (||) is a logical AND (&&). Also, your break; statement in that loop will ensure the loop only ever runs once (it doesn't break the outer while loop).

//...
      else {
        while (choice != 1 && choice != 2 && choice != 3) {
    
          printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
          scanf("%d", &choice);
      //  break; // This will end the `while` on the first loop, for ANY input!
      }
//...

With this, the while loop will run until any one of the three tests evaluates to false (i.e. if choice is any one of 1, 2 or 3).


Note: The above does not fully explain what happens with your example input (although I'm guessing the outer while loop ends and code that you haven't shown then displays the computer's choice); to do that, we need to see a bit more code, so that it is a minimal, compilable, reproducible example.

Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
0

I would recommend reworking the logic slightly to avoid calling scanf twice. You can do this something like this:

  choice = 0;
  while (choice != 1 || choice != 2 || choice != 3) 
      printf("Please choose 1 for ROCK, 2 for PAPER or 3 for SCISSORS.\n");
      scanf("%d", &choice);
    
      if ( choice == 1 ){
        printf("User choice is ROCK.\n");
      }
      else if ( choice == 2 ){
        printf("User choice is PAPER.\n");
      }
      else if ( choice == 3 ){
        printf("User choice is SCISSORS.\n");
      }  
      else {
        printf("Invalid choice.")
      }
  }
mrlindsey
  • 300
  • 2
  • 12