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 20:03

>>8
OK, this is one of the countless pitfalls of C++. The == operator is overloaded for string, however what happens here is that your "name" string object is implicitly casted into a temporary integer yielding the address of the "name" object which is then compared to the address of the string literal "Anonymous". Obviously this is not what you want, so you'd have to explicitly cast "Anonymous" into a string object. Well you see C++ is full of crap like this, so you should just use a proper language like C# or Java.

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