-1

I was writing c program on rock paper and scissor game but could not get the expected result.

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

int main()
{
    char player[50], choice[10];

    puts("Enter your name: ");
    gets(player);

    int player1 = 0, comp = 0;

    for (int i = 0; i < 3; i++)
    {
        srand(time(NULL));

        printf("%s: ", player);
        scanf("%s", choice);

        int computer = rand()%3;

        if (computer == 0){
            printf("computer: rock\n");
        } else if(computer == 1){
            printf("computer: paper\n");
        }else{
            printf("computer: scissor\n");
        }

        if (computer == 0 && choice == "paper"){
            player1++;
        }else if (computer == 0 && choice == "rock"){
            continue;
        }else if (computer == 0 && choice == "scissor"){
            comp++;
        }else if (computer == 1 && choice == "paper"){
            continue;
        }else if (computer == 1 && choice == "rock"){
            comp++;
        }else if (computer == 1 && choice == "scissor"){
            player1++;
        }else if (computer == 2 && choice == "paper"){
            player1++;
        }else if (computer == 2 && choice == "rock"){
            comp++;
        }else if (computer == 2 && choice == "scissor"){
            continue;
        }else {
            printf("Invalid Entry.");
        }
        printf("\n");
    }

    if (player1 > comp){
        printf("%s wins.", player);
    }else if (player1 == comp) {
        printf("%s and computer draws.", player);
    }else{
        printf("%s loses.", player);
    }
    
    return 0;
}

The program works but I am not getting the expecteed result in all 3 runs of the for loop I get only invalid entry as output due to which I can't count the score and the result as both player and comp variable are idle due to this.

output:

Enter your name:
Sam
Sam: rock
computer: scissor
Invalid Entry.   
Sam: scissor
computer: scissor
Invalid Entry.   
Sam: paper
computer: paper
Invalid Entry.
Sam and computer draws.

I have checked to best my skill and could not find any mistake I don't know why if else-if is acting weirdly, Help me to get correct output and explain me what the problem and where the mistake is.

Pallavarv
  • 13
  • 3
  • Numerous things wrong here, using `gets`, `==` to compare strings, `scanf` without checking return value, etc. – Govind Parmar Mar 25 '21 at 15:31
  • 1
    Your comparisons would be flagged by the compiler with `-Wall` (e.g.) _warning: comparison with string literal results in unspecified behavior [-Waddress] if (computer == 0 && choice == "paper") {_ – Craig Estey Mar 25 '21 at 15:32
  • When you do (e.g.) `choice == "paper"`, you are trying to compare the _address_ that is inside `choice` against the _address_ of the `"paper"` string literal. You want to compare the _contents_ of the strings. So, you want `strcmp` as Andy explained below. – Craig Estey Mar 25 '21 at 15:36
  • Looking at @CraigEstey's answer, as a beginner I'd enable all warnings, and maybe even turn warnings into errors, and work on fixing them all: `-Wall -Wextra -Werror`. You can even add `-pedantic` and `-pedantic-errors`. Your aim is not only to get your program to compile, but also have it do the right thing, and there are many traps in C, as you have and will learn. – Jan Christoph Terasa Mar 25 '21 at 15:53
  • Does my answer solve your problem? – Andy Sukowski-Bang Mar 26 '21 at 18:21

1 Answers1

2

You need to comare strings using strcmp from <string.h>. Note that strcmp returns 0 if the strings match.

#include <string.h>

int strcmp(const char *str1, const char *str2);

For example you would have to replace choice == "paper" with !strcmp(choise, "paper").


And you should also use fgets instead of gets.

char player[50];
fgets(str, 50, stdin);

Alternatively you could use and scanf (inferior in many cases).

char player[50];
scanf("%49[^\n]", player); /* take \0 into account */
Andy Sukowski-Bang
  • 1,402
  • 7
  • 20