I am failing to compare characters of a string in my program, I made a simpler version to showcase the problem:
#include <iostream>
using namespace std;
int main() {
string s = "Hello world!";
for(int i = 0; i<s.size(); i++) {
if(s[i] == "w") {
cout << "This is a w!" << endl;
}
}
}
It returns this error:
so.cpp:10:25: error: ISO C++ forbids comparison between pointer and integer [-fpermissive]
10 | if(s[i] == "w")
>Solution :
"w" is a pointer to the C string ['w', '\0'] in memory. You want to use single quotes instead so you are only comparing the character literal.
if (s[i] == 'w') {
cout << "This is a w!" << endl;
}