Thread: comparing user input with a string

  1. #1
    Registered User
    Join Date
    Jul 2011
    Posts
    62

    comparing user input with a string

    Hello again!
    Right now I am trying to compare user input with a prepared string. It is not recognizing though.

    I have tried these variants:
    Code:
    getline(cin, input); //input is the string that contains the user input
    std::string str2 = "something"; //assume that the user inputs something
    if(input==str2)
    //does not run
    Code:
    getline(cin, input); //input is the string that contains the user input
    std::string str2 = "something"; //assume that the user inputs something
    if(input.compare(str2)==0)
    //does not run
    Code:
    getline(cin, input); //input is the string that contains the user input
    //assume that the user inputs something
    if(input=="something")
    //does not run
    Code:
    getline(cin, input); //input is the string that contains the user input
    //assume that the user inputs something
    if(input==(string)"something")
    //does not run
    Code:
    getline(cin, input); //input is the string that contains the user input
    //assume that the user inputs something
    if(input.compare((string)"something")==0)
    //does not run
    I do not see how else can this be done.

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Are you completely sure that the input string contains exactly "something" .
    cout it to verify.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    @_@ thanks. I was using a hand-made
    Code:
    std::string operator&(std::string here)
    {
    getline(cin, here);
    return here;
    }
    and that was the bugging part. thanks!

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Why is that function the address-of operator? It has no reason to be since it's totally unrelated to what the operator does. This is why I dislike operator overloads so much.

  5. #5
    Registered User
    Join Date
    Jul 2011
    Posts
    62
    Quote Originally Posted by whiteflags View Post
    Why is that function the address-of operator? It has no reason to be since it's totally unrelated to what the operator does. This is why I dislike operator overloads so much.
    I personally use ~ for output and & for input (because thats what the symbols tell my head @_@ somehow I make connections between the symbols and I/O). I use & for pointers though, if so is needed.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Shingetsu Kurai
    I personally use ~ for output and & for input (because thats what the symbols tell my head @_@ somehow I make connections between the symbols and I/O).
    No! That is an abuse of operator overloading. Overload operator<< and operator>> instead because of the strong convention from the standard library (in which case you should also follow the convention for the parameters and return type), otherwise create named functions.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to store a user input string into dynamic array?
    By sethwb in forum C++ Programming
    Replies: 4
    Last Post: 04-25-2011, 04:48 AM
  2. user input pre-enter string?
    By mikeyp in forum C++ Programming
    Replies: 4
    Last Post: 11-25-2009, 02:59 PM
  3. Replies: 4
    Last Post: 06-04-2009, 01:36 AM
  4. comparing user input
    By lambs4 in forum C Programming
    Replies: 5
    Last Post: 12-15-2002, 10:28 AM
  5. user string input
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-13-2001, 07:47 AM

Tags for this Thread