0

How can I compare a single character from a string, and another string (which may or may not be greater than one character)

This program gives me almost 300 lines of random errors. The errors don't reference a specific line number either, just a lot of stuff about "char* ", "", or "std::to_string".

#include <iostream>
#include <string>

using std::cout;
using std::string;

int main() {
    string str = "MDCXIV";
    string test = "D";

    if (test == str[4]) {     // This line causes the problems
        cout << test << endl;
    }
    return 0;
}
5
  • 3
    Why not char test = 'D';?! Commented Sep 13, 2013 at 20:38
  • 1
    It's unclear whether you want to check if test ''contains'' str[4], or if it ''starts with'' str[4]. Commented Sep 13, 2013 at 20:42
  • You cannot compare a single character with another string which which may or may not be greater than one character long. This makes no sense what so ever. Why would you ever compare the 'single_char' to "multiple_chars"? This is cplusplus not python Commented Sep 13, 2013 at 20:43
  • @Smac89 Because "Multiple_chars" includes the possibility of a single char. I might be wrong, but it seems the OP is asking for something completely reasonable. He want the comparison to succeed if the string is length one and that char equal to the other char, Commented Sep 13, 2013 at 20:45
  • @john Yes multiple_chars can infact be made up of just a single_char. However there is a difference between 'g' and ['g'] Commented Sep 13, 2013 at 20:52

6 Answers 6

5

str[4] is a char type, which will not compare with a string.

Compare apples with apples.

Use

test[0] == str[4]

instead.

Sign up to request clarification or add additional context in comments.

3 Comments

Because I don't believe it's the answer the OP is looking for. He wants to compare a character to a string, not a character to the first character in a string.
@john He also mentioned that this gave him 300 lines of errors.
@Smac89 He's exagerating presumably. But some compilers do give lengthy errors on template code.
2

You need to convert str[4] (which is a char) to a string before you can compare it to another string. Here's a simple way to do this

if (test == string(1, str[4])) {

2 Comments

Note that test may be "one or more characters". In the case of more than one character, your solution would never return true.
@thelamb I can't disagree with you, I think that's what the OP wants.
1

You're comparing a char to a std::string, this is not a valid comparison. You're looking for std::string::find, as follows:

if( test.find( str[4] ) != std::string::npos ) cout << test << "\n";

Note that this will return true if test contains str[4].

Comments

1

You're mixing types. It doesn't know how to compare a string (test) to a char (str[4]).

If you change test to a char that will work fine. Or reference the specific character within test you want to compare such as if (test[0] == str[4]) it should compile and run.

However, as this is merely an example and not really the true question what you'll want to do is look at the functionality that the std::string class supplies

Comments

0

Also you need "D" to be a char value not a string value if you are comparing it like that.

std::string myString = "Hello World";
const char *myStringChars = myString.c_str();

You have to turn it into a char array before can access it. Unless you do.

str.at(i);

which you can also write as

str[i] <-- what you did.

Essentially, this all boils down to test needs to initialized like char test = 'D';

Final Output..

#include <iostream>
#include <string>

using std::cout;
using std::string;

int main() {
    string str = "MDCXIV";
    char test = 'D';

    if (test == str[4]) {     // This line causes NO problems
        cout << test << endl;
    }
    return 0;
}

Comments

0

I think you might be mixing python with c++. In c++ 'g' refers to a single character g not a string of length 1. "g" refers to an array (string) which is 1 character long and looks like ['g']. So as you can see, if you compare a single character to an array of characters no matter if the array is a single character long, this operation is not defined.

This will work if define it yourself by building a class which is able to compare string of one character long to a single character. Or just overload the == operator for doing just that

Example:

#include <iostream>
#include <string>

using std::cout;
using std::string;
using std::endl;

bool operator == ( const string &lh, const char &rh) {
    if (lh.length() == 1) return lh[0] == rh;
    return 0;
}

int main() {
    string str = "MDCXIV";
    string test = "D";

    if (test == str[4]) {
        cout << test << endl;
    }
    else cout << "Not a match\n";
    return 0;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.