-2

I am trying to check if the user string input (after lowercasing user input) matches with required three strings i.e. rock or paper or scissor. If it doesn't match the requirement the system will print It is a wrong input. Otherwise, I'll do something.

When I'm giving only one check without the logical || operator, it is working fine i.e. comparing the user input string with the required one. But when I'm using logical operator it is not working properly i.e. if I give right keyword, it is saying that it is a wrong input.

Being a beginner I couldn't figure out the possible reason after searching in StackOverflow also. Any help in advance. Thank you

#include <stdio.h>
#include <string.h>
#include <ctype.h>

int main()
{
    char game[40];
    printf("Enter one among rock, paper and scissor: ");
    scanf("%s", &game);
    for (int i = 0; game[i]; i++)
    {
        game[i] = tolower(game[i]);
    }
    if ((strcmp("rock", game) != 0) || (strcmp("paper", game) != 0) || (strcmp("scissor", game) != 0))
    {
        printf("You entered a wrong input\n");
    }
    else
    {
        /* Do Something */
    }
    
}
  • 5
    Try using '&&' instead. To elaborate on 'Why', I suggest reading the condition out loud. Right now it's: "Print 'Wrong Input' if: Not Rock OR Not Paper OR Not Scisssors." Since it can only be one of the three (if any), the condition will always return 'true', because at least 2 out of three are 'Not it'. – Refugnic Eternium Sep 23 '21 at 09:20
  • @RefugnicEternium I have tried it also. Though, I want any of the three to be true, so I think I should use the || (or) operator. – Adrija Roy Sep 23 '21 at 09:22
  • In that case, you need to check for 'equal' and not for 'not equal'. Please refer to Alex F's comment, which gives an example. – Refugnic Eternium Sep 23 '21 at 09:23
  • Any string will be different from _either_ `rock` or `paper`. – 500 - Internal Server Error Sep 23 '21 at 09:23
  • @AlexF Ok.... I will try and let you know. But where I have done wrong? Both should work same... – Adrija Roy Sep 23 '21 at 09:25
  • did you try to see in a debugger to see what is an actual content of `game` ? – Piotr Golacki Sep 23 '21 at 09:26
  • Aside: `scanf("%s", &game);` should be `scanf("%39s", game);` (two changes). Also, as the loop is dependant on a NUL terminator being present, initialise with `char game[40] = "";` – Weather Vane Sep 23 '21 at 09:29
  • Thanks to all of you. I understood where I was wrong. Thanks again. – Adrija Roy Sep 23 '21 at 09:35
  • 1
    We already answered this very question a couple of days ago. If you had bothered to read any of the answers you would have gotten your answer. – Lundin Sep 23 '21 at 10:55

1 Answers1

1

You've got your conditions mixed up.

If you want to 'do something', if it's any of these strings, you need to check for 'equal', as in:

if (!strcmp("rock", game) || !strcmp("paper", game) || !strcmp("scissor", game))
{
   //Do something
}
else
{
    printf("You entered a wrong input\n");
}

Alternatively you can do a cascade:

if (!strcmp("rock", game))
    //Do something for rock
else if (!strcmp("paper", game))
    //Do something for paper
else if (!strcmp("scissors", game))
    //Do something for scissors
else
    printf("Wrong input\n");

This has the same effect with the bonus effect of telling you exactly, which the user has input.

If you wanted to check whether it is NONE of the options, you'll need to use &&.

Refugnic Eternium
  • 4,089
  • 1
  • 15
  • 24