Rushat Rai
Rushat Rai

Reputation: 793

What's wrong with the conditions in this IF-ELSE?

I have written a program for a weird dice game which presumedly has buggy conditions in the IF-ELSE portion. I am pasting a working code snippet which replicates the problem.

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

int main()
{
    int rollTotal = 11;
    int rollTotal2 = 10;
    char userGuess[5] = "l";

    if ((userGuess == "h" && rollTotal2 > rollTotal) || (userGuess == "e" && rollTotal2 == rollTotal) || (userGuess == "l" && rollTotal2 < rollTotal))
    {
        printf("You win :D \n");
    }
    else
    {
        printf("You lose D: \n");
    }

    return 0;
}

Expected output: "You win :D" | Actual output: "You lose D:"

I thought it was a problem with the conditions. So, I changed the conditions to (userGuess == 'h' && rollTotal2 > rollTotal) and so on (I changed the double inverted commas to a single inverted commas). That gave me the same output along with warning: comparison between pointer and integer. This lead me to a confusing SO Q&A which I couldn't make anything of because of me not knowing those functions.

That lead me to the current state of my program which is the one pasted above (I reverted back to double inverted commas). I continue to believe the IF-ELSE's conditions are buggy. It is still giving me the same buggy output along with Warning: comparison with string literals results in unspecified behaviour. This lead me to yet another fruitless endeavor.

This error is probably due to my lack of knowledge of pointers and such and is most likely extremely dumb. Please help me out here.

Some information which may help-

IDE: Code::Blocks

Compiler: GCC

OS: Linux

Upvotes: 0

Views: 333

Answers (3)

tdao
tdao

Reputation: 17713

userGuess == "h" (and userGuess == "e")

You compare string literals using operator == which is not a valid string comparison in C - you should use standard library function for string comparison strcmp instead1.

Otherwise, make userGuess as single char variable instead of array of char, and use single character (single quote) for comparison, such as

userGuess == 'h'

1 As it stands, the comparison userGuess == "h" compiles but should give you an warning if you turn on gcc -Wall compiler flag, for example:

warning: comparison with string literal results in unspecified behavior [-Waddress]

It means (as mentioned by Jonathan Leffler's comment below), you are not comparing the strings itself but instead comparing the two pointers (userGuess and "l" are pointing to the same location) - which is valid but generally meaningless.

Upvotes: 8

Vimal Bhaskar
Vimal Bhaskar

Reputation: 778

Please understand the concept of char datatype. If you want to compare only one character then please do not declare the variable as a string and compare it like a character. Please find the code below.

#include <stdio.h>
#include <stdlib.h>
int main()
{
int rollTotal = 11;
int rollTotal2 = 10;
char userGuess = 'l';
 if ((userGuess == 'h' && rollTotal2 > rollTotal) || (userGuess == 'e' && rollTotal2 == rollTotal) || (userGuess == 'l' && rollTotal2 < rollTotal))
    {
    printf("You win :D \n");
}
else
{
    printf("You lose D: \n");
}
getchar();
return 0;
}

if You want to compare string instead please use strcmp() function to compare the strings and add the header file #include in your code.

Upvotes: 2

ramdav
ramdav

Reputation: 98

You are comparing the address of your array userGuess to the address of the Strings "h" and "e".

The soltution to this would be using

*userGuess == 'h'

Name of an Array is the address of this array, same as a pointer. 'h' is a literal character rathen then a string.

Upvotes: 0

Related Questions