2

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;
}
Donut
  • 110,061
  • 20
  • 134
  • 146
Chetxn04
  • 43
  • 2
  • 3
    "Y" etc. are string constants. You are trying to compare a const NUL,terminated char array, ('string') with a char. Try 'Y' instead. – Martin James Sep 29 '21 at 13:41
  • 2
    Once you fix your char-versus-string problem, you're going to have the problem that your second `scanf` call is going to read, not the answer to the science question, but the newline that you typed after answering the maths question. `scanf` sucks, and `%c` sucks worse. Either don't use `"%c"` at all, or change it to `" %c"` (note the extra space). See [here](https://stackoverflow.com/questions/5240789) for explanation. – Steve Summit Sep 29 '21 at 13:47
  • Furthermore you do not want to use `%c` here, or at least not that way. If you reply with Y Enter, the first `scanf` will read the `'Y'` character, and the second one will read the `'\n'`... – Serge Ballesta Sep 29 '21 at 13:47
  • Basically C has no pre-made string class and that string class, which C doesn't have, is not `char`. You need to study arrays, then pointers, then strings, in that order. – Lundin Sep 29 '21 at 14:27
  • Uh, or maybe the intention here is just to compare single characters? I might have closed this pre-maturely... I'll re-open. – Lundin Sep 29 '21 at 14:30

1 Answers1

2

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.

  • I tried it as u told and it doesnt show the warning but after taking the first input it doesnt even ask for the second input and just ends the program – Chetxn04 Sep 29 '21 at 14:25
  • 1
    @Chetxn04 That's what we were trying to warn you about in the comments to your question. "`scanf` sucks, and `%c` sucks worse." Not your fault that they suck, and not your fault that you tried to use them, but now you know. :-) – Steve Summit Sep 29 '21 at 14:38