Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

noob question c++

Name: Anonymous 2009-07-12 13:15

Why doesn't this work?

#include <iostream>
using namespace std;
int main(int argc,char** argv){
   string name;
   cout<<"What is your name?"<<endl;
   cin>>name;
   if(name=="Anonymous")
   cout<<"Aw don't be shy..."<<endl;
   else
   cout<<"Hello "<<name<<"!"<<endl
   return 0;
}

Name: Anonymous 2009-07-12 21:05

>>11
So to restate what happens: string overloads == and the literal "Anonymous" is casted to string because string has a constructor that takes char*. The comparison works.  I have two questions then. 
1) If <, <=, > and >= are overridden, what purpose does string.compare serve other than redundancy?  Either use .compare and .equals (like java) or only use the overloaded operators.  The only reason I can think of is that they wanted to reimplemented C's strcmp for C++, but in that case why wouldn't they also re-implement functions such as atoi, and strtof which have to use c_str().

2) Is it standard to use == for string equality? I have string.compare(var)==0 all over some code that will be reviewed by people experienced in c++ ...

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List