I can't understand why this simple code, that is storing an answer from the user is giving me back this error.
- I would like to save the answer into a variable
q_ai
and be able to use this variable in an if.
this below is my code:
//INPUT TRY 0
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//setting game:
char q_ai; //want to play against ai? question about ai.
int main(void)
{
printf("Do you want to play vs AI? \n Y/N? \n ");
scanf("%c",q_ai);
printf("q_ai == %c\n",q_ai);
printf("sizeof(q_ai): %d",sizeof(q_ai));
if(q_ai=="y") {
printf("you will play against AI");
}
if(q_ai=="n") {
printf("you will play 1v1");
}
}
How can I solve this problem? thanks
CodePudding user response:
What you have right now is comparing a character to pointers to strings ("y"
and "n"
). You need to compare the character q_ai
to characters ('y'
and 'n'
).
if(q_ai=='y') {
printf("you will play against AI");
}
if(q_ai=='n') {
printf("you will play 1v1");
}