Like Tree1Likes
  • 1 Post By laserlight

comparing user input with a string

This is a discussion on comparing user input with a string within the C++ Programming forums, part of the General Programming Boards category; Hello again! Right now I am trying to compare user input with a prepared string. It is not recognizing though. ...

  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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,505
    Are you completely sure that the input string contains exactly "something" .
    cout it to verify.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,752
    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
    22,159
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 05: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, 02: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, 08:47 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21