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!
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.
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" .
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).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With