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:49

>>11 and >>12 here again. I'm going to rant even fucking more about Java's == operator. It makes intuitive sense to compare objects with ==; you're testing whether they're the same instance. But with Strings, everything breaks. If you create several Strings from a literal, they will compare equal. If the user inputs the string, it will not compare equal to the literal. Now compound this with the fact that String is immutable. If you clone either of these, they WILL compare equal to the original, because clone is "return this" for immutable types. The whole thing is so fucking broken that == should not exist at all for objects that can be created as literals (String, Integer, etc), and should not exist at all for immutable types because the whole notion of 'instance' should not exist for immutable types.


To answer OP's question, your cin<<name is catching the trailing newline. It will work if you compare it to ("Anonymous" + endl).

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