Matching substr() results

This is a discussion on Matching substr() results within the C++ Programming forums, part of the General Programming Boards category; Hey! I've got a program that... well, check out the code below (what I currently have): Code: if(userInput.substr(0, 3) == ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    118

    Matching substr() results

    Hey!

    I've got a program that... well, check out the code below (what I currently have):

    Code:
    if(userInput.substr(0, 3) == 'cd ' || 'Cd ' || 'cD ' || 'CD ')
    {
    	nDirLoc = userInput.substr(3, 255);
    }
    As you can see, I have an if loop that I want to check to see the first three characters of a string with to see if they match what I had put in.

    GCC complains:

    no match for 'operator==' in 'std::basic_string<_CharT, _Traits, _Alloc>::substr(typename _Alloc::size_type, typename _Alloc::size_type) const [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](0u, 3u) == 6513696'
    How can I go about checking to see whether or not the substring results are that of what I am expecting?

    Thanks for the help!
    FlyingIsFun1217

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    1) ' delimits character literals, not string literals.

    2) || tests completely independent conditions. Your if statement, if it used string literals, would test
    if the initial 3 characters of userInput are "cd "
    OR
    if the string literal "Cd " evaluates to true
    OR
    if the string literal "cD " evaluates to true
    OR
    if the string literal "CD " evaluates to true.

    You should get some string-to-lower function, apply it to the substring, and simply compare once against "cd ". There's one in Boost. (Or this one.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    118
    Alright, thanks for pointing out the ' and " difference. Guess I was too tired, and forgot to check!

    With the string-to-lower function, would that be collecting input, and lowercasing it all?

    Thanks again, always very helpful here!
    FlyingIsFun1217

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by FlyingIsFun1217 View Post
    Alright, thanks for pointing out the ' and " difference. Guess I was too tired, and forgot to check!

    With the string-to-lower function, would that be collecting input, and lowercasing it all?

    Thanks again, always very helpful here!
    FlyingIsFun1217
    You don't want to lower-case all of the input, just the command that you need to compare case-insensitively [in fact, you may also use a case-insensitive compare, but that's likely to just lower-case the strign anyways].

    The reason you don't just lowercase the whole thing is that some commands may differentiate between options that are upper case and options that are lower-case. gcc for example do not treat -C and -c as the same thing, nor -e and -E or -s and -S. Likewise, it's nice to be able to use mixed case when creating filenames and directory names - even if the OS doesn't make the difference into different files, it's easier to read if the case is "what you expect".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 06-01-2009, 07:54 PM
  2. Matching numbers
    By kirksson in forum C Programming
    Replies: 7
    Last Post: 07-23-2008, 01:51 PM
  3. 72hour GDC Results
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 07-05-2004, 11:46 PM
  4. Going out of scope
    By nickname_changed in forum C++ Programming
    Replies: 9
    Last Post: 10-12-2003, 06:27 PM
  5. Same seed for srand yields different results
    By codegirl in forum C++ Programming
    Replies: 3
    Last Post: 06-23-2003, 02:39 PM

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