This came up in another thread. I gave some advice that I'm not longer sure of. Rather than hijacking that thread, I figured I'd start my own. I advised against using the == in that thread. The context was this:
string subChoice ="";
getline(cin,subChoice);
if (subChoice == "100")
{
// code
}
Ancient Dragon said the above was fine and that compare was unnecessary. So I wondered if maybe == was only bad when comparing two string variables, so I did a little test program and it worked:
#include <iostream>
#include <string>
using namespace std;
int main ()
{
string a = "hello";
string b = "hello";
if (a == b)
cout << "a and b are the same." << endl;
return 0;
}
The line displayed to the screen. So my question is when is it bad to use == when comparing strings? I thought the program above was not supposed to work. Apparently I was mistaken. If it's always fine to use ==, do we ever need to use the "compare" function?