0

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

Community
  • 1
  • 1
Rushat Rai
  • 743
  • 2
  • 15
  • 31

3 Answers3

8

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.

artm
  • 17,291
  • 6
  • 38
  • 54
  • Since they are *single* characters -- why not suggest using **single quotes** so that the comparison will be valid as written? (e.g. make it a character comparison instead of a string comparison) – David C. Rankin Jan 04 '17 at 06:21
  • That's a fair edit. Don't forget `char userGuess = '1';` to insure he understands `userGuess` needs to be a character as well (otherwise, just deference `userGuess` for the comparison, e.g. `*userGuess == 'h'`, etc.. for comparison of the first character. `:)` – David C. Rankin Jan 04 '17 at 06:26
  • It's not so much invalid to compare string literals using `==` as essentially invariably meaningless to do so. It certainly is never going to produce an answer of 'true' when one operand is a string literal and the other is a variable. Maybe this is quibbling, but ... – Jonathan Leffler Jan 04 '17 at 06:26
2

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.

Vimal Bhaskar
  • 748
  • 1
  • 5
  • 17
0

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.

ramdav
  • 98
  • 5
  • Why not simply `userGuess == 'h'`? – RoadRunner Jan 04 '17 at 07:05
  • I don't see why the '*' is required. It works perfectly without it :) – Rushat Rai Jan 04 '17 at 07:55
  • @MythicCocoa-- I believe the point here is that, since you had `char userGuess[5]`, you could have used `*userGuess == 'h'`, or equivalently `userGuess[0] == 'h'`. You can't use `userGuess == 'h'` unless you change the declaration to `char userGuess`, i.e., not an array. – ad absurdum Jan 04 '17 at 14:13
  • @MythicCocoa userGuess is the name of the Variable the Variable is of type char []. The name of a Variable of an Array Type without the braces is equvalent to a pointer to the first element say the array is placed on your stack at address 0x200, issuing `userGuess == 'h' ` compares 0x200 to 0x68 – ramdav Jan 05 '17 at 11:57