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.
input!='R' || input!='r'
is always true. Rest is irrelevant. I suspect code should beinput!='R' && input!='r' && ...
if(input=='Q' || input=='q')
test makes more sense after thewhile()
loop.