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
Aucun commentaire:
Enregistrer un commentaire