Warning about comparing a pointer and an an integer

Viewed 67

I'm learning how to program, and I wrote the program below based on the following prompt:

Q) Take input from the user if he/she has passed his science or maths test . If he has passed both then show that he won 50 rupees and if he won either of them then show that he won 15 rupees .

However, I'm getting a warning about comparing a pointer and an integer. What does this mean, and how can I resolve it?

#include<stdio.h>

int main()
{
char maths, science;

printf("Have you passed maths exam ? If yes type Y and if no type N .\n");

scanf("%c", &maths);

printf("Have you passed science exam ? If yes type Y and if no type N .\n");

scanf("%c", &science);

if (maths == "Y" && science == "Y")
{
    printf("Congrats !! You will get 50 Rupees , please collect it from your teacher.\n");
}

if (maths == "Y" && science == "N")
{
    printf("Congrats !! You will get 15 Rupees , please collect it from your teacher.\n");
}

if (maths == "N" && science == "Y")
{
    printf("Congrats !! You will get 15 Rupees , please collect it from your teacher.\n");
}

if (maths == "N" && science == "N") 
{
    printf("U get nothing , Sorry. \n");
}

return 0;
}
1 Answers

You need to use single quotes everywhere while comparing with character example as following, if (maths == 'Y' && science == 'Y')

From mentioned code, if (maths == "Y" && science == "Y") => here "Y" is treated as text string and character variable maths is compared to the pointer which is pointing to string. So it is giving error.

Related