-1

I'm creating a rock, paper, scissors code for a school lab. I'm just trying to test for invalid entries, but I get stuck in a continuous while loop despite entering a valid character.

printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
scanf("%c", &input);
if(input=='Q' || input=='q') {
     return 0;
}
while(input!='R' || input!='r' || input!='P' || input!='p' || input!='S' || input!='s' || input!='Q' || input!='q') {
        printf("\nInvalid entry! Please enter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
        scanf(" %c", &input);
}

Any help would be appreciated!

2
  • 3
    input!='R' || input!='r' is always true. Rest is irrelevant. I suspect code should be input!='R' && input!='r' && ... Sep 12, 2018 at 23:09
  • Minor issue: the if(input=='Q' || input=='q') test makes more sense after the while() loop. Sep 12, 2018 at 23:54

2 Answers 2

2

You need to change all the OR operators in the while loop to AND operators. If they are OR operators, all inputs would be true.

`

printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
scanf("%c", &input);
if(input=='Q' || input=='q') {
     return 0;
}
while(input!='R' && input!='r' && input!='P' && input!='p' && input!='S' && input!='s' && input!='Q' && input!='q') {
        printf("\nInvalid entry! Please enter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
        scanf(" %c", &input);
}

`

1

It seems you've got a classical logic bug in your code. It took me a while to catch it too, because using or (||) seems to make sense. In this case, though, if you think about it, the computer is following your instructions correctly and never allowing you to leave the loop. What you really mean is that if your input is different than 'R' and (&&) 'r' and 'P' and 'p'... it is not valid.

On another, unrelated topic, it is probably a good idea to always check the return value of your input function calls. In this case you may want to write something like

while (1) { /* infinite loop */
    printf("\nEnter (r)ock, (p)aper, or (s)cissors. Enter (q) to quit.");
    if (scanf(" %c", &c) != EOF) {
        if (c == 'q' || c == 'Q') {
            break;
        } else if (c == 'r' || c == 'R') {
            /* rock */
        } else if (/* paper, etc... */) {
            /* ... */
        } else {
            /* you can say here that the input is not valid */
        }
    } else {
        printf("\nBye!\n");
        break;
    }
}

As a 'bonus' if you're using a *NIX terminal (think Linux or BSD) you'll be able to leave the loop by typing Ctrl-D (^D), which will cause the scanf to return EOF. Remember to always be very careful if you ever write an infinite loop (like this while(1) loop on my example). You'll have to provide some way to get out of those loops.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.