CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Help again =/

  1. #1
    Join Date
    Apr 2010
    Posts
    60

    Help again =/

    Sorry for so many posts..

    i have

    "if ( answer == "Yes")" But that only detects "Yes" not "yes" only capital Y, how do i add other Words to this?, so that it can detect Yes and yes?

  2. #2
    Join Date
    Apr 2008
    Posts
    725

    Re: Help again =/

    Code:
    if( answer == "Yes" || answer == "yes" )
    if you look at some regex you could do something like [Yy][Ee][Ss]

  3. #3
    Join Date
    Apr 2010
    Posts
    60

    Re: Help again =/

    Thanks.. "||" = or?

  4. #4
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help again =/

    Correct, || is logical or. (As opposed to bitwise or, which is |.)

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Help again =/

    Quote Originally Posted by UpcomingChris View Post
    Thanks.. "||" = or?
    I thought you were learning C++, are you reading a book? I'm asking because these are chapter 1 questions. I don't have anything against answering these questions, but maybe learning the basics before trying to write a program might be a good idea?

  6. #6
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help again =/

    What is answer? Perhaps you could use stricmp which will do a case-insensitive string comparison.

  7. #7
    Join Date
    Aug 2008
    Posts
    902

    Re: Help again =/

    Quote Originally Posted by GCDEF View Post
    What is answer? Perhaps you could use stricmp which will do a case-insensitive string comparison.
    Code:
    string ToLower(const string str)
    {
    	string temp;
    	for(int i=0; i<str.length(); i++)
    	{
    		temp += tolower(str[i]);
    	}
    	return temp;
    }
    Code:
    if(ToLower(answer) == "yes")
    {
    
    }

  8. #8
    Join Date
    Apr 2010
    Posts
    60

    Re: Help again =/

    Quote Originally Posted by monarch_dodra View Post
    I thought you were learning C++, are you reading a book? I'm asking because these are chapter 1 questions. I don't have anything against answering these questions, but maybe learning the basics before trying to write a program might be a good idea?
    Not reading a book or anything, im the type of person that wants to Jump in and get started right away, and learn on the go, thats just how i do things :P

  9. #9
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: Help again =/

    Quote Originally Posted by UpcomingChris View Post
    Not reading a book or anything, im the type of person that wants to Jump in and get started right away, and learn on the go, thats just how i do things :P
    Probably not the best strategy for C++.

  10. #10
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help again =/

    ^That sort of approach might teach you what the basic constructs are, but it'll take you a long longer to learn how to use them effectively if you don't have a solid understanding of the available tools given in a structured manner.

    Quote Originally Posted by Chris_F View Post
    Code:
    string ToLower(const string str)
    {
    	string temp;
    	for(int i=0; i<str.length(); i++)
    	{
    		temp += tolower(str[i]);
    	}
    	return temp;
    }
    Code:
    if(ToLower(answer) == "yes")
    {
    
    }
    I'll counter with:
    Code:
    string ToLower(const string str)
    {
    	string temp(str.size());
            transform(str.begin(),str.end(),temp.begin(),ptr_fun(::tolower));
    }
    Or, possibly faster on some implementations:
    Code:
    string ToLower(const string str)
    {
    	string temp;
            temp.reserve(str.size());
            transform(str.begin(),str.end(),back_inserter(temp),ptr_fun(::tolower));
    }

  11. #11
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Help again =/

    Quote Originally Posted by Lindley View Post
    Code:
    string ToLower(const string str)
    Well if you are going to pass by value, why stop there?

    Code:
    string ToLower(string str)
    {
        transform(str.begin(), str.end(), str.begin(), ptr_fun(::tolower));
        return str;
    }
    ...

    Yes, I realize that was probably a typo (not even by you), and I'm being an ***.

    I was going to make the same transform comment, but I had a feeling the only person here who would bot have understood is the OP.

    To the OP: Getting your hands dirty and doing some C++ is always a great way to learn how to structure your algorithms, give you a better understanding of what you know, and mak you learn a function or two...

    But only by reading a book will you really learn and progress. Failing to do so will also probably leave you with some profound miss-understandings about a very tricky language.

  12. #12
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: Help again =/

    Good point.

  13. #13
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: Help again =/

    Quote Originally Posted by Lindley View Post
    Good point.
    Well, not that good actually, you have to pay for a copy of the values of str. When you pass by reference, tmp just needs to be sized (or reserved), at str's size. The actual values of str don't need to be copied.

    The tmp.reserve(str.size()) with a back_inserter method is probably the best algorithm.

  14. #14
    Join Date
    Aug 2008
    Posts
    902

    Re: Help again =/

    Quote Originally Posted by monarch_dodra View Post
    Well if you are going to pass by value, why stop there?

    ...

    Yes, I realize that was probably a typo (not even by you), and I'm being an ***.
    I'm confused.

  15. #15
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Help again =/

    Quote Originally Posted by UpcomingChris View Post
    Not reading a book or anything, im the type of person that wants to Jump in and get started right away, and learn on the go, thats just how i do things :P
    I don't know what other topics you've studied, but learning how to write programs in C++ isn't a "learn on the go" subject. I will just state plainly that you can't learn C++ that way. Do yourself a favor and get a good C++ book . I teach C++, and there is no way you're going to learn C++ properly without formal guidance from a book or several books.

    If you're asking chapter 1 questions, you will be constantly asked to get a book. For one, it is frustrating for others to help someone who is asking basic questions about C++, questions that are answered on the first 10 or so pages of any beginner book.

    Secondly, you may receive answers that are in terms of C++, very simple, and you may have no idea what we're talking about because you are learning haphazardly with no direction, and you didn't learn the simple topics. This thread is an example, where you're asking what is "||".

    Third, without books, you will put together (and it will happen) a program that may be so convoluted, but it just happens to "work". Then if you have a problem with the program, you will post it here, and none of us will even attempt to make heads or tails of it (maybe someone will, but it won't be fun). In reality, that very same program could have been written more coherently and simply if a proper approach to learning the language was done.

    Regards,

    Paul McKenzie

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured