Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

c++ compile error: ISO C++ forbids comparison between pointer and integer

I am trying an example from Bjarne Stroustrup's C++ book, third edition. While implementing a rather simple function, I get the following compile time error:

error: ISO C++ forbids comparison between pointer and integer 

What could be causing this? Here is the code. The error is in the if line:

#include <iostream> #include <string> using namespace std; bool accept() {     cout << "Do you want to proceed (y or n)?\n";     char answer;     cin >> answer;     if (answer == "y") return true;     return false; } 

Thanks!

like image 804
Morlock Avatar asked Feb 15 '10 02:02

Morlock


People also ask

What does ISO C++ forbids comparison between pointer and integer mean?

In C++, single apostrophes are used to represent characters, not strings. We utilize double quotes symbols to epitomize the pointer. After compiling the programs in C++, we get different errors.

How do you compare integers and pointers?

Also note that in your comparison *s == "a" it is actually the "a" which is the pointer, and *s which is the integer... The * dereferences s , which results in the char (an integer) stored at the address pointed to by s . The string literal, however, acts as a pointer to the first character of the string "a" .


1 Answers

You have two ways to fix this. The preferred way is to use:

string answer; 

(instead of char). The other possible way to fix it is:

if (answer == 'y') ... 

(note single quotes instead of double, representing a char constant).

like image 81
Chris Jester-Young Avatar answered Oct 22 '22 23:10

Chris Jester-Young