i get this error when i try executing it,

no match for the operator ==

now i understand you can't compare a string with a char, so i tried other method like strcmp, covert a to char, but none worked.

note: coverting d to string is not an option.

#include <iostream>
#include<string>

using namespace std;

int main(int argc, char *argv[])
{
    int i;
    char d='h';    
string s="this is a string";
string a;
a=s.substr(1,1);
if(a==d)  
cout<<"String compared\n";

cout<<a<<"\t"<<s<<"\n";
cin>>i;

    return 0;
}
Member Avatar

diafol

So the best place to post this was the Community Center?

commented: For some reason, no match for operator sounds spot on here. Hope they find their way around the forums. +0

I would be guessing the goal here, but when I want to see if a string is in another string I refresh my memory at
http://www.cplusplus.com/reference/string/string/find/

In parting, I am unaware of the Bug Language. I would have to guess you meant C++ and don't want to upset you over code formatting but line 2 might get you a F in some computer classes.

You should describe what you're TRYING to do in simple English sentences. I'm guessing here, but it LOOKS like you MAY be trying to test whether the character at index 1 of "this is a string" is the letter 'h'. If so...

#include <iostream>
#include<string>
using namespace std;

int main(int argc, char *argv[])
{
    char d='h';    
    string s="this is a string";
    if(s[1]==d)  
        cout<<"The character at index 1 is 'h'" << endl;
    return 0;
}

If this is NOT what you are trying to do, please explain what you are trying to do. But the compiler sees a string and a character and can't guess what you want. Hence the error.

You can also create a string from the char variable d and compare two strings, as so...

#include <iostream>
#include<string>

using namespace std;

int main(int argc, char *argv[])
{
    char d='h';    
    string s="this is a string";
    string a = s.substr(1,1);
    if(a==string(1,d))  
        cout<<"The character at index 1 is 'h'" << endl;
    return 0;
}

string(1,d) creates the string "h" from the character 'h'. See http://www.cplusplus.com/reference/string/string/string/

Look at constructor #6, the "fill" constructor.

string (size_t n, char c);
Fills the string with n consecutive copies of character c.