0
char grade;
cin>>grade;       
if(grade ==  "A"){
  multOfGradeHour += 4.0*creditHour;
}

compare problem I checked at google but did not found any good solution. when i enter code here run this code compiler shows me an error.c++ forbids comparison between pointers and integer.i don't understand this because i know nothing about pointers.i tried many things but i did not find any solution for this.anyone who know what to do?

brc-dd
  • 6,933
  • 3
  • 26
  • 51
  • 2
    `'A'` is a character, `"A"` is a string – Passerby Dec 28 '21 at 13:20
  • 1
    Does this answer your question? [c++ compile error: ISO C++ forbids comparison between pointer and integer](https://stackoverflow.com/questions/2263681/c-compile-error-iso-c-forbids-comparison-between-pointer-and-integer) – mostsignificant Dec 28 '21 at 13:30

2 Answers2

1

You have to change "A" which is a const char* (pointer) to a char A (which is converted to integer in your original code, therefore the confusing error message from the compiler):

char grade;
cin>>grade;       
if(grade == 'A'){
  multOfGradeHour += 4.0*creditHour;
}
mostsignificant
  • 302
  • 1
  • 7
0

The problem is that "A" is a string literal with type const char[2] which decays to a const char *(that is, to a pointer to a const char) due to type decay.

This means that you are trying to compare a variable named grade which is of type char with a const char * which is what the error says.

Solution

To solve this replace if(grade == "A") with:

if(grade ==  'A') //note A is surrounded by single quotes ' ' this time instead of double quotes " "

This solution works because this time, 'A' is a character literal and has type char which is the same type that variable grade has. So, the type of grade and 'A' are the same and hence this works.

Anoop Rana
  • 19,715
  • 4
  • 12
  • 33